

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="沧海横流，尽显英雄本色；激浊扬清，正是猛士当时">
  <meta name="author" content="closer">
  <meta name="keywords" content="">
  
  <title>玩转Linux操作系统 - closer的自留地</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.6.0/styles/androidstudio.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"blog.zsaa.top","root":"/","version":"1.8.10","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"always","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"baidu":"608f2baddd361128381ad2bf9377bf89","google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"YzLqNtMw1YEwwACli1FUsIUM-gzGzoHsz","app_key":"HLUt5izfTvTcbEbOrA59W92a","server_url":"https://yzlqntmw.lc-cn-n1-shared.com"}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>Hello</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="玩转Linux操作系统">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-02-23 15:05" pubdate>
        2020年2月23日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      15.1k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      211
       分钟
    </span>
  

  
  
    
      <!-- LeanCloud 统计文章PV -->
      <span id="leancloud-page-views-container" class="post-meta" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="leancloud-page-views"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">玩转Linux操作系统</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：2020年9月20日 凌晨
                
              </p>
            
            <div class="markdown-body">
              <blockquote>
<p><strong>本文转载于GitHub项目<a target="_blank" rel="noopener" href="https://github.com/jackfrued/Python-100-Days">Python - 100天从新手到大师</a></strong></p>
</blockquote>
<blockquote>
<p>说明：本文中对Linux命令的讲解都是基于名为CentOS的Linux发行版本，我自己使用的是阿里云服务器，系统版本为CentOS Linux release 7.6.1810。不同的Linux发行版本在Shell命令和工具程序上会有一些差别，但是这些差别是很小的。</p>
</blockquote>
<h3 id="一：-操作系统发展史"><a href="#一：-操作系统发展史" class="headerlink" title="一： 操作系统发展史"></a>一： 操作系统发展史</h3><p>只有硬件没有软件的计算机系统被称之为“裸机”，我们很难用“裸机”来完成计算机日常的工作（如存储和运算），所以必须用特定的软件来控制硬件的工作。最靠近计算机硬件的软件是系统软件，其中最为重要的就是“操作系统”。“操作系统”是控制和管理整个计算机硬件和软件资源、实现资源分配和任务调配、为系统用户以及其他软件提供接口和环境的程序的集合。</p>
<h4 id="1-1-没有操作系统（手工操作）"><a href="#1-1-没有操作系统（手工操作）" class="headerlink" title="1.1 没有操作系统（手工操作）"></a>1.1 没有操作系统（手工操作）</h4><p>在计算机诞生之初没有操作系统的年代，人们先把程序纸带（或卡片）装上计算机，然后启动输入机把程序送入计算机，接着通过控制台开关启动程序运行。当程序执行完毕，打印机输出计算的结果，用户卸下并取走纸带（或卡片）。第二个用户上机，重复同样的步骤。在整个过程中用户独占机器，CPU等待手工操作，资源利用率极低。</p>
<h4 id="1-2-批处理系统"><a href="#1-2-批处理系统" class="headerlink" title="1.2 批处理系统"></a>1.2 批处理系统</h4><p>首先启动计算机上的一个监督程序，在监督程序的控制下，计算机能够自动的、成批的处理一个或多个用户的作业。完成一批作业后，监督程度又从输入机读取作业存入磁带机。按照上面的步骤重复处理任务。监督程序不停的处理各个作业，实现了作业的自动转接，减少了作业的建立时间和手工操作时间，提高了计算机资源的利用率。 批处理系统又可以分为单道批处理系统、多道批处理系统、联机批处理系统、脱机批处理系统。</p>
<h4 id="1-3-分时系统和实时系统"><a href="#1-3-分时系统和实时系统" class="headerlink" title="1.3 分时系统和实时系统"></a>1.3 分时系统和实时系统</h4><p>分时系统是把处理器的运行时间分成很短的时间片，按时间片轮流把处理机分配给各联机作业使用。 若某个作业在分配给它的时间片内不能完成其计算，则该作业暂时中断，把处理机让给另一作业使用，等待下一轮调度时再继续其运行。由于计算机速度很快，作业运行轮转得很快，给每个用户的感觉是他独占了一台计算机。而每个用户可以通过自己的终端向系统发出各种操作控制命令，在充分的人机交互情况下，完成作业的运行。为了解决分时系统不能及时响应用户指令的情况，又出现了能够在在严格的时间范围内完成事件处理，及时响应随机外部事件的实时系统。</p>
<h4 id="1-4-通用操作系统"><a href="#1-4-通用操作系统" class="headerlink" title="1.4 通用操作系统"></a>1.4 通用操作系统</h4><ol>
<li><p>1960s：IBM的System/360系列的机器有了统一的操作系统OS/360。</p>
</li>
<li><p>1965年：AT&amp;T的贝尔实验室加入GE和MIT的合作计划开始开发MULTICS。</p>
</li>
<li><p>1969年：MULTICS项目失败，Ken Tompson赋闲在家，为了玩“Space Travel”游戏用汇编语言在当时已经被淘汰的PDP-7上开发了Unics。</p>
<blockquote>
<p>注：很难想象，Unix这么伟大的系统，居然是一个赋闲在家的程序员（关键是老婆回娘家还带上了孩子）在一台被淘汰的设备上为了玩游戏开发出来的。</p>
</blockquote>
</li>
<li><p>1970年~1971年：Ken Tompson和Dennis Ritchie用B语言在PDP-11上重写了Unics，并在Brian Kernighan的建议下将其更名为Unix。</p>
<p><img src="https://closer_laps.coding.net/p/picture/d/picture/git/raw/master/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/Linux/ken-and-dennis-pdp-11.png" srcset="/img/loading.gif" lazyload></p>
</li>
<li><p>1972年~1973年：Dennis Ritchie发明了C语言来取代可移植性较差的B语言，并开启了用C语言重写Unix的工作。</p>
</li>
<li><p>1974年：Unix推出了里程碑意义的第5版，几乎完全用C语言来实现。</p>
</li>
<li><p>1979年：从Unix第7版开始，AT&amp;T发布新的使用条款，将Unix私有化。</p>
</li>
<li><p>1987年：Andrew S. Tanenbaum教授为了能在课堂上为学生讲解操作系统运作的细节，决定在不使用任何AT&amp;T的源代码前提下，自行开发与Unix兼容的操作系统以避免版权上的争议，该系统被命名为Minix。</p>
<p><img src="https://closer_laps.coding.net/p/picture/d/picture/git/raw/master/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/Linux/andrew.jpg" srcset="/img/loading.gif" lazyload></p>
</li>
<li><p>1991年：Linus Torvalds就读于芬兰赫尔辛基大学期间，尝试在Minix上做一些开发工作，但因为Minix只是作为教学用途的操作系统，功能并不强大，为了方便在学校的新闻组和邮件系统中读写和下载文件，Linus编写了磁盘驱动程序和文件系统，这些东西形成了Linux系统内核的雏形。</p>
<p><img src="https://closer_laps.coding.net/p/picture/d/picture/git/raw/master/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/Linux/linus.png" srcset="/img/loading.gif" lazyload></p>
</li>
</ol>
<p>下图是Unix操作系统家族的图谱。</p>
<p><img src="https://closer_laps.coding.net/p/picture/d/picture/git/raw/master/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/Linux/history-of-unix.png" srcset="/img/loading.gif" lazyload></p>
<h3 id="二：-Linux概述"><a href="#二：-Linux概述" class="headerlink" title="二： Linux概述"></a>二： Linux概述</h3><p>Linux是一个通用操作系统。一个操作系统要负责任务调度、内存分配、处理外围设备I/O等操作。操作系统通常由内核（运行其他程序，管理像磁盘、打印机等硬件设备的核心程序）和系统程序（设备驱动、底层库、shell、服务程序等）两部分组成。</p>
<p>Linux内核是芬兰人Linus Torvalds开发的，于1991年9月发布。而Linux操作系统作为Internet时代的产物，它是由全世界许多开发者共同合作开发的，是一个自由的操作系统（注意自由和免费并不是同一个概念，想了解二者的差别可以<a target="_blank" rel="noopener" href="https://www.debian.org/intro/free">点击这里</a>）。</p>
<h3 id="三：-Linux系统优点"><a href="#三：-Linux系统优点" class="headerlink" title="三： Linux系统优点"></a>三： Linux系统优点</h3><ol>
<li>通用操作系统，不跟特定的硬件绑定。</li>
<li>用C语言编写，可移植性强，有内核编程接口。</li>
<li>支持多用户和多任务，支持安全的分层文件系统。</li>
<li>大量的实用程序，完善的网络功能以及强大的支持文档。</li>
<li>可靠的安全性和良好的稳定性，对开发者更友好。</li>
</ol>
<h3 id="四：-Linux系统发行版本"><a href="#四：-Linux系统发行版本" class="headerlink" title="四： Linux系统发行版本"></a>四： Linux系统发行版本</h3><ol>
<li><a target="_blank" rel="noopener" href="https://www.redhat.com/en">Redhat</a></li>
<li><a target="_blank" rel="noopener" href="https://www.ubuntu.com/">Ubuntu</a></li>
<li><a target="_blank" rel="noopener" href="https://www.centos.org/">CentOS</a></li>
<li><a target="_blank" rel="noopener" href="https://getfedora.org/">Fedora</a></li>
<li><a target="_blank" rel="noopener" href="https://www.debian.org/">Debian</a></li>
<li><a target="_blank" rel="noopener" href="https://www.opensuse.org/">openSUSE</a></li>
</ol>
<h3 id="五：-基础命令"><a href="#五：-基础命令" class="headerlink" title="五： 基础命令"></a>五： 基础命令</h3><p>Linux系统的命令通常都是如下所示的格式：</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">命令名称 [命名参数] [命令对象]<br></code></pre></div></td></tr></table></figure>

<ol>
<li><p>获取登录信息 - <strong>w</strong> / <strong>who</strong> / <strong>last</strong>/ <strong>lastb</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# w<br> 23:31:16 up 12:16,  2 users,  load average: 0.00, 0.01, 0.05<br>USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT<br>root     pts/0    182.139.66.250   23:03    4.00s  0.02s  0.00s w<br>jackfrue pts/1    182.139.66.250   23:26    3:56   0.00s  0.00s -bash<br>[root ~]# who<br>root     pts/0        2018-04-12 23:03 (182.139.66.250)<br>jackfrued pts/1        2018-04-12 23:26 (182.139.66.250)<br>[root ~]# who am i<br>root     pts/0        2018-04-12 23:03 (182.139.66.250)<br>[root ~]# who mom likes<br>root     pts/0        2018-04-12 23:03 (182.139.66.250)<br>[root ~]# last<br>root     pts/0        117.136.63.184   Sun May 26 18:57   still logged in   <br>reboot   system boot  3.10.0-957.10.1. Mon May 27 02:52 - 19:10  (-7:-42)   <br>root     pts/4        117.136.63.184   Sun May 26 18:51 - crash  (08:01)    <br>root     pts/4        117.136.63.184   Sun May 26 18:49 - 18:49  (00:00)    <br>root     pts/3        117.136.63.183   Sun May 26 18:35 - crash  (08:17)    <br>root     pts/2        117.136.63.183   Sun May 26 18:34 - crash  (08:17)    <br>root     pts/0        117.136.63.183   Sun May 26 18:10 - crash  (08:42)    <br></code></pre></div></td></tr></table></figure></li>
<li><p>查看自己使用的Shell - <strong>ps</strong>。</p>
<p>Shell也被称为“壳”或“壳程序”，它是用户与操作系统内核交流的翻译官，简单的说就是人与计算机交互的界面和接口。目前很多Linux系统默认的Shell都是bash（<u>B</u>ourne <u>A</u>gain <u>SH</u>ell），因为它可以使用tab键进行命令和路径补全、可以保存历史命令、可以方便的配置环境变量以及执行批处理操作。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# ps<br>  PID TTY          TIME CMD<br> 3531 pts/0    00:00:00 bash<br> 3553 pts/0    00:00:00 ps<br></code></pre></div></td></tr></table></figure></li>
<li><p>查看命令的说明和位置 - <strong>whatis</strong> / <strong>which</strong> / <strong>whereis</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# whatis ps<br>ps (1)        - report a snapshot of the current processes.<br>[root ~]# whatis python<br>python (1)    - an interpreted, interactive, object-oriented programming language<br>[root ~]# whereis ps<br>ps: /usr/bin/ps /usr/share/man/man1/ps.1.gz<br>[root ~]# whereis python<br>python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz<br>[root ~]# which ps<br>/usr/bin/ps<br>[root ~]# which python<br>/usr/bin/python<br></code></pre></div></td></tr></table></figure></li>
<li><p>清除屏幕上显示的内容 - <strong>clear</strong>。</p>
</li>
<li><p>查看帮助文档 - <strong>man</strong> / <strong>info</strong> / <strong>help</strong> / <strong>apropos</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root@izwz97tbgo9lkabnat2lo8z ~]# ps --help<br>Usage:<br> ps [options]<br> Try &#x27;ps --help &lt;simple|list|output|threads|misc|all&gt;&#x27;<br>  or &#x27;ps --help &lt;s|l|o|t|m|a&gt;&#x27;<br> for additional help text.<br>For more details see ps(1).<br>[root@izwz97tbgo9lkabnat2lo8z ~]# man ps<br>PS(1)                                User Commands                                PS(1)<br>NAME<br>       ps - report a snapshot of the current processes.<br>SYNOPSIS<br>       ps [options]<br>DESCRIPTION<br>...<br></code></pre></div></td></tr></table></figure></li>
<li><p>查看系统和主机名 - <strong>uname</strong> / <strong>hostname</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root@izwz97tbgo9lkabnat2lo8z ~]# uname<br>Linux<br>[root@izwz97tbgo9lkabnat2lo8z ~]# hostname<br>izwz97tbgo9lkabnat2lo8z<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# cat /etc/centos-release<br>CentOS Linux release 7.6.1810 (Core)<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>说明：<code>cat</code>是连接文件内容并打印到标准输出的命令，后面会讲到该命令；<code>/etc</code>是Linux系统上的一个非常重要的目录，它保存了很多的配置文件；<code>centos-release</code>是该目录下的一个文件，因为我自己使用的Linux发行版本是CentOS 7.6，因此这里会有一个这样的文件。</p>
</blockquote>
</li>
<li><p>时间和日期 - <strong>date</strong> / <strong>cal</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root@iZwz97tbgo9lkabnat2lo8Z ~]# date<br>Wed Jun 20 12:53:19 CST 2018<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# cal<br>      June 2018<br>Su Mo Tu We Th Fr Sa<br>                1  2<br> 3  4  5  6  7  8  9<br>10 11 12 13 14 15 16<br>17 18 19 20 21 22 23<br>24 25 26 27 28 29 30<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# cal 5 2017<br>      May 2017<br>Su Mo Tu We Th Fr Sa<br>    1  2  3  4  5  6<br> 7  8  9 10 11 12 13<br>14 15 16 17 18 19 20<br>21 22 23 24 25 26 27<br>28 29 30 31<br></code></pre></div></td></tr></table></figure></li>
<li><p>重启和关机 - <strong>reboot</strong> / <strong>shutdown</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# shutdown -h +5<br>Shutdown scheduled for Sun 2019-05-26 19:34:27 CST, use &#x27;shutdown -c&#x27; to cancel.<br>[root ~]# <br>Broadcast message from root (Sun 2019-05-26 19:29:27 CST):<br><br>The system is going down for power-off at Sun 2019-05-26 19:34:27 CST!<br>[root ~]# shutdown -c<br><br>Broadcast message from root (Sun 2019-05-26 19:30:22 CST):<br><br>The system shutdown has been cancelled at Sun 2019-05-26 19:31:22 CST!<br>[root ~]# shutdown -r 23:58<br>Shutdown scheduled for Sun 2019-05-26 23:58:00 CST, use &#x27;shutdown -c&#x27; to cancel.<br>[root ~]# shutdown -c<br><br>Broadcast message from root (Sun 2019-05-26 19:31:06 CST):<br><br>The system shutdown has been cancelled at Sun 2019-05-26 19:32:06 CST!<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>说明：在执行<code>shutdown</code>命令时会向登录系统的用户发出警告，可以在命令后面跟上警告消息来替换默认的警告消息，也可以在<code>-h</code>参数后通过<code>now</code>来表示立刻关机。</p>
</blockquote>
</li>
<li><p>退出登录 -  <strong>exit</strong> / <strong>logout</strong>。</p>
</li>
<li><p>查看历史命令 - <strong>history</strong>。</p>
</li>
</ol>
  <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root@iZwz97tbgo9lkabnat2lo8Z ~]# history<br>...<br>452  ls<br>453  cd Python-3.6.5/<br>454  clear<br>455  history<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# !454<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>说明：查看到历史命令之后，可以用<code>!历史命令编号</code>来重新执行该命令；通过<code>history -c</code>可以清除历史命令。</p>
</blockquote>
<h3 id="六：-实用程序"><a href="#六：-实用程序" class="headerlink" title="六： 实用程序"></a>六： 实用程序</h3><h4 id="6-1-文件和文件夹操作"><a href="#6-1-文件和文件夹操作" class="headerlink" title="6.1 文件和文件夹操作"></a>6.1 文件和文件夹操作</h4><ol>
<li><p>创建/删除空目录 - <strong>mkdir</strong> / <strong>rmdir</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# mkdir abc<br>[root ~]# mkdir -p xyz/abc<br>[root ~]# rmdir abc<br></code></pre></div></td></tr></table></figure></li>
<li><p>创建/删除文件 - <strong>touch</strong> / <strong>rm</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# touch readme.txt<br>[root ~]# touch error.txt<br>[root ~]# rm error.txt<br>rm: remove regular empty file ‘error.txt’? y<br>[root ~]# rm -rf xyz<br></code></pre></div></td></tr></table></figure>

<ul>
<li><code>touch</code>命令用于创建空白文件或修改文件时间。在Linux系统中一个文件有三种时间：<ul>
<li>更改内容的时间 - mtime。</li>
<li>更改权限的时间 - ctime。</li>
<li>最后访问时间 - atime。</li>
</ul>
</li>
<li><code>rm</code>的几个重要参数：<ul>
<li><code>-i</code>：交互式删除，每个删除项都会进行询问。</li>
<li><code>-r</code>：删除目录并递归的删除目录中的文件和目录。</li>
<li><code>-f</code>：强制删除，忽略不存在的文件，没有任何提示。</li>
</ul>
</li>
</ul>
</li>
<li><p>切换和查看当前工作目录 - <strong>cd</strong> / <strong>pwd</strong>。</p>
<blockquote>
<p>说明：<code>cd</code>命令后面可以跟相对路径（以当前路径作为参照）或绝对路径（以<code>/</code>开头）来切换到指定的目录，也可以用<code>cd ..</code>来返回上一级目录。请大家想一想，如果要返回到上上一级目录应该给<code>cd</code>命令加上什么样的参数呢？</p>
</blockquote>
</li>
<li><p>查看目录内容 - <strong>ls</strong>。</p>
<ul>
<li><code>-l</code>：以长格式查看文件和目录。</li>
<li><code>-a</code>：显示以点开头的文件和目录（隐藏文件）。</li>
<li><code>-R</code>：遇到目录要进行递归展开（继续列出目录下面的文件和目录）。</li>
<li><code>-d</code>：只列出目录，不列出其他内容。</li>
<li><code>-S</code> / <code>-t</code>：按大小/时间排序。</li>
</ul>
</li>
<li><p>查看文件内容 - <strong>cat</strong> / <strong>tac</strong> / <strong>head</strong> / <strong>tail</strong> / <strong>more</strong> / <strong>less</strong> / <strong>rev</strong> / <strong>od</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# wget http://www.sohu.com/ -O sohu.html<br>--2018-06-20 18:42:34--  http://www.sohu.com/<br>Resolving www.sohu.com (www.sohu.com)... 14.18.240.6<br>Connecting to www.sohu.com (www.sohu.com)|14.18.240.6|:80... connected.<br>HTTP request sent, awaiting response... 200 OK<br>Length: 212527 (208K) [text/html]<br>Saving to: ‘sohu.html’<br><span class="hljs-meta">100%</span><span class="bash">[==================================================&gt;] 212,527     --.-K/s   <span class="hljs-keyword">in</span> 0.03s</span><br>2018-06-20 18:42:34 (7.48 MB/s) - ‘sohu.html’ saved [212527/212527]<br>[root ~]# cat sohu.html<br>...<br>[root ~]# head -10 sohu.html<br>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>&lt;title&gt;搜狐&lt;/title&gt;<br>&lt;meta name=&quot;Keywords&quot; content=&quot;搜狐,门户网站,新媒体,网络媒体,新闻,财经,体育,娱乐,时尚,汽车,房产,科技,图片,论坛,微博,博客,视频,电影,电视剧&quot;/&gt;<br>&lt;meta name=&quot;Description&quot; content=&quot;搜狐网为用户提供24小时不间断的最新资讯，及搜索、邮件等网络服务。内容包括全球热点事件、突发新闻、时事评论、热播影视剧、体育赛事、行业动态、生活服务信息，以及论坛、博客、微博、我的搜狐等互动空间。&quot; /&gt;<br>&lt;meta name=&quot;shenma-site-verification&quot; content=&quot;1237e4d02a3d8d73e96cbd97b699e9c3_1504254750&quot;&gt;<br>&lt;meta charset=&quot;utf-8&quot;/&gt;<br>&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=Edge,chrome=1&quot;/&gt;<br>[root ~]# tail -2 sohu.html<br>&lt;/body&gt;<br>&lt;/html&gt;<br>[root ~]# less sohu.html<br>...<br>[root ~]# cat -n sohu.html | more<br>...<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>说明：上面用到了一个名为<code>wget</code>的命令，它是一个网络下载器程序，可以从指定的URL下载资源。</p>
</blockquote>
</li>
<li><p>拷贝/移动文件 - <strong>cp</strong> / <strong>mv</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# mkdir backup<br>[root ~]# cp sohu.html backup/<br>[root ~]# cd backup<br>[root backup]# ls<br>sohu.html<br>[root backup]# mv sohu.html sohu_index.html<br>[root backup]# ls<br>sohu_index.html<br></code></pre></div></td></tr></table></figure></li>
<li><p>文件重命名 - <strong>rename</strong>。</p>
</li>
</ol>
  <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root@iZwz97tbgo9lkabnat2lo8Z ~]# rename .htm .html *.htm<br></code></pre></div></td></tr></table></figure>

<ol start="8">
<li><p>查找文件和查找内容 - <strong>find</strong> / <strong>grep</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root@iZwz97tbgo9lkabnat2lo8Z ~]# find / -name &quot;*.html&quot;<br>/root/sohu.html<br>/root/backup/sohu_index.html<br>[root@izwz97tbgo9lkabnat2lo8z ~]# find . -atime 7 -type f -print<br>[root@izwz97tbgo9lkabnat2lo8z ~]# find . -type f -size +2k<br>[root@izwz97tbgo9lkabnat2lo8z ~]# find . -type f -name &quot;*.swp&quot; -delete<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# grep &quot;&lt;script&gt;&quot; sohu.html -n<br>20:&lt;script&gt;<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# grep -E \&lt;\/?script.*\&gt; sohu.html -n<br>20:&lt;script&gt;<br>22:&lt;/script&gt;<br>24:&lt;script src=&quot;//statics.itc.cn/web/v3/static/js/es5-shim-08e41cfc3e.min.js&quot;&gt;&lt;/script&gt;<br>25:&lt;script src=&quot;//statics.itc.cn/web/v3/static/js/es5-sham-1d5fa1124b.min.js&quot;&gt;&lt;/script&gt;<br>26:&lt;script src=&quot;//statics.itc.cn/web/v3/static/js/html5shiv-21fc8c2ba6.js&quot;&gt;&lt;/script&gt;<br>29:&lt;script type=&quot;text/javascript&quot;&gt;<br>52:&lt;/script&gt;<br>...<br></code></pre></div></td></tr></table></figure>
<blockquote>
<p>说明：<code>grep</code>在搜索字符串时可以使用正则表达式，如果需要使用正则表达式可以用<code>grep -E</code>或者直接使用<code>egrep</code>。</p>
</blockquote>
</li>
<li><p>创建链接和查看链接 - <strong>ln</strong> / <strong>readlink</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html<br>-rw-r--r-- 1 root root 212131 Jun 20 19:15 sohu.html<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# ln /root/sohu.html /root/backup/sohu_backup<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html<br>-rw-r--r-- 2 root root 212131 Jun 20 19:15 sohu.html<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# ln /root/sohu.html /root/backup/sohu_backup2<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html<br>-rw-r--r-- 3 root root 212131 Jun 20 19:15 sohu.html<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# ln -s /etc/centos-release sysinfo<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sysinfo<br>lrwxrwxrwx 1 root root 19 Jun 20 19:21 sysinfo -&gt; /etc/centos-release<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# cat sysinfo<br>CentOS Linux release 7.4.1708 (Core)<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# cat /etc/centos-release<br>CentOS Linux release 7.4.1708 (Core)<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>说明：链接可以分为硬链接和软链接（符号链接）。硬链接可以认为是一个指向文件数据的指针，就像Python中对象的引用计数，每添加一个硬链接，文件的对应链接数就增加1，只有当文件的链接数为0时，文件所对应的存储空间才有可能被其他文件覆盖。我们平常删除文件时其实并没有删除硬盘上的数据，我们删除的只是一个指针，或者说是数据的一条使用记录，所以类似于“文件粉碎机”之类的软件在“粉碎”文件时除了删除文件指针，还会在文件对应的存储区域填入数据来保证文件无法再恢复。软链接类似于Windows系统下的快捷方式，当软链接链接的文件被删除时，软链接也就失效了。</p>
</blockquote>
</li>
<li><p>压缩/解压缩和归档/解归档 - <strong>gzip</strong> / <strong>gunzip</strong> / <strong>xz</strong>。</p>
</li>
</ol>
  <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root@iZwz97tbgo9lkabnat2lo8Z ~]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz<br>--2018-06-20 19:29:59--  http://download.redis.io/releases/redis-4.0.10.tar.gz<br>Resolving download.redis.io (download.redis.io)... 109.74.203.151<br>Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.<br>HTTP request sent, awaiting response... 200 OK<br>Length: 1738465 (1.7M) [application/x-gzip]<br>Saving to: ‘redis-4.0.10.tar.gz’<br><span class="hljs-meta">100%</span><span class="bash">[==================================================&gt;] 1,738,465   70.1KB/s   <span class="hljs-keyword">in</span> 74s</span><br>2018-06-20 19:31:14 (22.9 KB/s) - ‘redis-4.0.10.tar.gz’ saved [1738465/1738465]<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis*<br>redis-4.0.10.tar.gz<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# gunzip redis-4.0.10.tar.gz<br>[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis*<br>redis-4.0.10.tar<br></code></pre></div></td></tr></table></figure>

<ol start="11">
<li>归档和解归档 - <strong>tar</strong>。</li>
</ol>
   <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root@iZwz97tbgo9lkabnat2lo8Z ~]# tar -xvf redis-4.0.10.tar<br>redis-4.0.10/<br>redis-4.0.10/.gitignore<br>redis-4.0.10/00-RELEASENOTES<br>redis-4.0.10/BUGS<br>redis-4.0.10/CONTRIBUTING<br>redis-4.0.10/COPYING<br>redis-4.0.10/INSTALL<br>redis-4.0.10/MANIFESTO<br>redis-4.0.10/Makefile<br>redis-4.0.10/README.md<br>redis-4.0.10/deps/<br>redis-4.0.10/deps/Makefile<br>redis-4.0.10/deps/README.md<br>...<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>说明：归档（也称为创建归档）和解归档都使用<code>tar</code>命令，通常创建归档需要<code>-cvf</code>三个参数，其中<code>c</code>表示创建（create），<code>v</code>表示显示创建归档详情（verbose），<code>f</code>表示指定归档的文件（file）；解归档需要加上<code>-xvf</code>参数，其中<code>x</code>表示抽取（extract），其他两个参数跟创建归档相同。</p>
</blockquote>
<ol start="12">
<li>将标准输入转成命令行参数 - <strong>xargs</strong>。</li>
</ol>
<p>   下面的命令会将查找当前路径下的html文件，然后通过<code>xargs</code>将这些文件作为参数传给<code>rm</code>命令，实现查找并删除文件的操作。</p>
   <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root@iZwz97tbgo9lkabnat2lo8Z ~]# find . -type f -name &quot;*.html&quot; | xargs rm -f<br></code></pre></div></td></tr></table></figure>

<p>   下面的命令将a.txt文件中的多行内容变成一行输出到b.txt文件中，其中<code>&lt;</code>表示从a.txt中读取输入，<code>&gt;</code>表示将命令的执行结果输出到b.txt中。</p>
   <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root@iZwz97tbgo9lkabnat2lo8Z ~]# xargs &lt; a.txt &gt; b.txt<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>说明：这个命令就像上面演示的那样常在管道（实现进程间通信的一种方式）和重定向（重新指定输入输出的位置）操作中用到，后面的内容中会讲到管道操作和输入输出重定向操作。</p>
</blockquote>
<ol start="13">
<li><p>显示文件或目录 - <strong>basename</strong> / <strong>dirname</strong>。</p>
</li>
<li><p>其他相关工具。 </p>
</li>
</ol>
<ul>
<li><strong>sort</strong> - 对内容排序</li>
<li><strong>uniq</strong> - 去掉相邻重复内容</li>
<li><strong>tr</strong> - 替换指定内容为新内容</li>
<li><strong>cut</strong> / <strong>paste</strong> - 剪切/黏贴内容</li>
<li><strong>split</strong> - 拆分文件</li>
<li><strong>file</strong> - 判断文件类型</li>
<li><strong>wc</strong> - 统计文件行数、单词数、字节数</li>
<li><strong>iconv</strong> - 编码转换</li>
</ul>
   <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# cat foo.txt<br>grape<br>apple<br>pitaya<br>[root ~]# cat bar.txt<br>100<br>200<br>300<br>400<br>[root ~]# paste foo.txt bar.txt<br>grape   100<br>apple   200<br>pitaya  300<br>        400<br>[root ~]# paste foo.txt bar.txt &gt; hello.txt<br>[root ~]# cut -b 4-8 hello.txt<br>pe      10<br>le      20<br>aya     3<br>0<br>[root ~]# cat hello.txt | tr &#x27;\t&#x27; &#x27;,&#x27;<br>grape,100<br>apple,200<br>pitaya,300<br>,400<br>[root ~]# split -l 100 sohu.html hello<br>[root ~]# wget https://www.baidu.com/img/bd_logo1.png<br>[root ~]# file bd_logo1.png<br>bd_logo1.png: PNG image data, 540 x 258, 8-bit colormap, non-interlaced<br>[root ~]# wc sohu.html<br>  2979   6355 212527 sohu.html<br>[root ~]# wc -l sohu.html<br>2979 sohu.html<br>[root ~]# wget http://www.qq.com -O qq.html<br>[root ~]# iconv -f gb2312 -t utf-8 qq.html<br></code></pre></div></td></tr></table></figure>

<h4 id="6-2-管道和重定向"><a href="#6-2-管道和重定向" class="headerlink" title="6.2 管道和重定向"></a>6.2 管道和重定向</h4><ol>
<li><p>管道的使用 - **|**。</p>
<p>例子：查找当前目录下文件个数。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# find ./ | wc -l<br>6152<br></code></pre></div></td></tr></table></figure>

<p>例子：列出当前路径下的文件和文件夹，给每一项加一个编号。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# ls | cat -n<br>     1  dump.rdb<br>     2  mongodb-3.6.5<br>     3  Python-3.6.5<br>     4  redis-3.2.11<br>     5  redis.conf<br></code></pre></div></td></tr></table></figure>

<p>例子：查找record.log中包含AAA，但不包含BBB的记录的总数</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# cat record.log | grep AAA | grep -v BBB | wc -l<br></code></pre></div></td></tr></table></figure></li>
<li><p>输出重定向和错误重定向 - <strong>&gt;</strong> / <strong>&gt;&gt;</strong> / **2&gt;**。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# cat readme.txt<br>banana<br>apple<br>grape<br>apple<br>grape<br>watermelon<br>pear<br>pitaya<br>[root ~]# cat readme.txt | sort | uniq &gt; result.txt<br>[root ~]# cat result.txt<br>apple<br>banana<br>grape<br>pear<br>pitaya<br>watermelon<br></code></pre></div></td></tr></table></figure></li>
<li><p>输入重定向 - **&lt;**。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# echo &#x27;hello, world!&#x27; &gt; hello.txt<br>[root ~]# wall &lt; hello.txt<br>[root ~]#<br>Broadcast message from root (Wed Jun 20 19:43:05 2018):<br>hello, world!<br>[root ~]# echo &#x27;I will show you some code.&#x27; &gt;&gt; hello.txt<br>[root ~]# wall &lt; hello.txt<br>[root ~]#<br>Broadcast message from root (Wed Jun 20 19:43:55 2018):<br>hello, world!<br>I will show you some code.<br></code></pre></div></td></tr></table></figure></li>
<li><p>多重定向 - <strong>tee</strong>。</p>
<p>下面的命令除了在终端显示命令<code>ls</code>的结果之外，还会追加输出到<code>ls.txt</code>文件中。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# ls | tee -a ls.txt<br></code></pre></div></td></tr></table></figure></li>
</ol>
<h4 id="6-3-别名"><a href="#6-3-别名" class="headerlink" title="6.3 别名"></a>6.3 别名</h4><ol>
<li><p><strong>alias</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# alias ll=&#x27;ls -l&#x27;<br>[root ~]# alias frm=&#x27;rm -rf&#x27;<br>[root ~]# ll<br>...<br>drwxr-xr-x  2 root       root   4096 Jun 20 12:52 abc<br>...<br>[root ~]# frm abc<br></code></pre></div></td></tr></table></figure></li>
<li><p><strong>unalias</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# unalias frm<br>[root ~]# frm sohu.html<br>-bash: frm: command not found<br></code></pre></div></td></tr></table></figure></li>
</ol>
<h4 id="6-4-文本处理"><a href="#6-4-文本处理" class="headerlink" title="6.4 文本处理"></a>6.4 文本处理</h4><ol>
<li><p>字符流编辑器 - <strong>sed</strong>。</p>
<p>sed是操作、过滤和转换文本内容的工具。假设有一个名为fruit.txt的文件，内容如下所示。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# cat -n fruit.txt <br>     1  banana<br>     2  grape<br>     3  apple<br>     4  watermelon<br>     5  orange<br></code></pre></div></td></tr></table></figure>

<p>接下来，我们在第2行后面添加一个pitaya。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# sed &#x27;2a pitaya&#x27; fruit.txt <br>banana<br>grape<br>pitaya<br>apple<br>watermelon<br>orange<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>注意：刚才的命令和之前我们讲过的很多命令一样并没有改变fruit.txt文件，而是将添加了新行的内容输出到终端中，如果想保存到fruit.txt中，可以使用输出重定向操作。</p>
</blockquote>
<p>在第2行前面插入一个waxberry。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# sed &#x27;2i waxberry&#x27; fruit.txt<br>banana<br>waxberry<br>grape<br>apple<br>watermelon<br>orange<br></code></pre></div></td></tr></table></figure>

<p>删除第3行。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# sed &#x27;3d&#x27; fruit.txt<br>banana<br>grape<br>watermelon<br>orange<br></code></pre></div></td></tr></table></figure>

<p>删除第2行到第4行。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# sed &#x27;2,4d&#x27; fruit.txt<br>banana<br>orange<br></code></pre></div></td></tr></table></figure>

<p>将文本中的字符a替换为@。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# sed &#x27;s#a#@#&#x27; fruit.txt <br>b@nana<br>gr@pe<br>@pple<br>w@termelon<br>or@nge<br></code></pre></div></td></tr></table></figure>

<p>将文本中的字符a替换为@，使用全局模式。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# sed &#x27;s#a#@#g&#x27; fruit.txt <br>b@n@n@<br>gr@pe<br>@pple<br>w@termelon<br>or@nge<br></code></pre></div></td></tr></table></figure>

<p>先cd目录，将后缀为.cfg的文件里面所有老内容替换为新内容。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">sed -i &quot;s/老内容/新内容/g&quot; `find . -type f -name &quot;*.cfg&quot;`<br></code></pre></div></td></tr></table></figure></li>
<li><p>模式匹配和处理语言 - <strong>awk</strong>。</p>
<p>awk是一种编程语言，也是Linux系统中处理文本最为强大的工具，它的作者之一和现在的维护者就是之前提到过的Brian Kernighan（ken和dmr最亲密的伙伴）。通过该命令可以从文本中提取出指定的列、用正则表达式从文本中取出我们想要的内容、显示指定的行以及进行统计和运算，总之它非常强大。</p>
<p>假设有一个名为fruit2.txt的文件，内容如下所示。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# cat fruit2.txt <br>1       banana      120<br>2       grape       500<br>3       apple       1230<br>4       watermelon  80<br>5       orange      400<br></code></pre></div></td></tr></table></figure>

<p>显示文件的第3行。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# awk &#x27;NR==3&#x27; fruit2.txt <br>3       apple       1230<br></code></pre></div></td></tr></table></figure>

<p>显示文件的第2列。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# awk &#x27;&#123;print $2&#125;&#x27; fruit2.txt <br>banana<br>grape<br>apple<br>watermelon<br>orange<br></code></pre></div></td></tr></table></figure>

<p>显示文件的最后一列。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# awk &#x27;&#123;print $NF&#125;&#x27; fruit2.txt <br>120<br>500<br>1230<br>80<br>400<br></code></pre></div></td></tr></table></figure>

<p>输出末尾数字大于等于300的行。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# awk &#x27;&#123;if($3 &gt;= 300) &#123;print $0&#125;&#125;&#x27; fruit2.txt <br>2       grape       500<br>3       apple       1230<br>5       orange      400<br></code></pre></div></td></tr></table></figure>

<p>上面展示的只是awk命令的冰山一角，更多的内容留给读者自己在实践中去探索。</p>
</li>
</ol>
<h3 id="七：-用户管理"><a href="#七：-用户管理" class="headerlink" title="七： 用户管理"></a>七： 用户管理</h3><ol>
<li><p>创建和删除用户 - <strong>useradd</strong> / <strong>userdel</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root home]# useradd hellokitty<br>[root home]# userdel hellokitty<br></code></pre></div></td></tr></table></figure>

<ul>
<li><code>-d</code> - 创建用户时为用户指定用户主目录</li>
<li><code>-g</code> - 创建用户时指定用户所属的用户组</li>
</ul>
</li>
<li><p>创建和删除用户组 - <strong>groupadd</strong> / <strong>groupdel</strong>。</p>
<blockquote>
<p>说明：用户组主要是为了方便对一个组里面所有用户的管理。</p>
</blockquote>
</li>
<li><p>修改密码 - <strong>passwd</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# passwd hellokitty<br>New password: <br>Retype new password: <br>passwd: all authentication tokens updated successfully.<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>说明：输入密码和确认密码没有回显且必须一气呵成的输入完成（不能使用退格键），密码和确认密码需要一致。如果使用<code>passwd</code>命令时没有指定命令作用的对象，则表示要修改当前用户的密码。如果想批量修改用户密码，可以使用<code>chpasswd</code>命令。</p>
</blockquote>
<ul>
<li><code>-l</code> / <code>-u</code> - 锁定/解锁用户。</li>
<li><code>-d</code> - 清除用户密码。</li>
<li><code>-e</code> - 设置密码立即过期，用户登录时会强制要求修改密码。</li>
<li><code>-i</code> - 设置密码过期多少天以后禁用该用户。</li>
</ul>
</li>
<li><p>查看和修改密码有效期 - <strong>chage</strong>。</p>
<p>设置hellokitty用户100天后必须修改密码，过期前15天通知该用户，过期后15天禁用该用户。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">chage -M 100 -W 15 -I 15 hellokitty<br></code></pre></div></td></tr></table></figure></li>
<li><p>切换用户 - <strong>su</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# su hellokitty<br>[hellokitty root]$<br></code></pre></div></td></tr></table></figure></li>
<li><p>以管理员身份执行命令 - <strong>sudo</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[hellokitty ~]$ ls /root<br>ls: cannot open directory /root: Permission denied<br>[hellokitty ~]$ sudo ls /root<br>[sudo] password for hellokitty:<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p><strong>说明</strong>：如果希望用户能够以管理员身份执行命令，用户必须要出现在sudoers名单中，sudoers文件在 <code>/etc</code>目录下，如果希望直接编辑该文件也可以使用下面的命令。</p>
</blockquote>
</li>
<li><p>编辑sudoers文件 - <strong>visudo</strong>。</p>
<p>这里使用的编辑器是vi，关于vi的知识在后面有讲解。该文件的部分内容如下所示：</p>
<figure class="highlight pgsql"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs pgsql">## Allow root <span class="hljs-keyword">to</span> run <span class="hljs-keyword">any</span> commands anywhere <br>root    <span class="hljs-keyword">ALL</span>=(<span class="hljs-keyword">ALL</span>)   <span class="hljs-keyword">ALL</span><br><br>## Allows members <span class="hljs-keyword">of</span> the <span class="hljs-string">&#x27;sys&#x27;</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">to</span> run networking, software, <br>## service management apps <span class="hljs-keyword">and</span> more.<br># %sys <span class="hljs-keyword">ALL</span> = NETWORKING, SOFTWARE, SERVICES, <span class="hljs-keyword">STORAGE</span>, DELEGATING, PROCESSES, LOCATE, DRIVERS<br>## Allows people <span class="hljs-keyword">in</span> <span class="hljs-keyword">group</span> wheel <span class="hljs-keyword">to</span> run <span class="hljs-keyword">all</span> commands<br>%wheel  <span class="hljs-keyword">ALL</span>=(<span class="hljs-keyword">ALL</span>)   <span class="hljs-keyword">ALL</span><br><br>## Same thing <span class="hljs-keyword">without</span> a <span class="hljs-keyword">password</span><br># %wheel    <span class="hljs-keyword">ALL</span>=(<span class="hljs-keyword">ALL</span>)   NOPASSWD: <span class="hljs-keyword">ALL</span><br><br>## Allows members <span class="hljs-keyword">of</span> the users <span class="hljs-keyword">group</span> <span class="hljs-keyword">to</span> mount <span class="hljs-keyword">and</span> unmount the<br>## cdrom <span class="hljs-keyword">as</span> root<br># %users  <span class="hljs-keyword">ALL</span>=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom<br><br>## Allows members <span class="hljs-keyword">of</span> the users <span class="hljs-keyword">group</span> <span class="hljs-keyword">to</span> shutdown this <span class="hljs-keyword">system</span><br># %users  localhost=/sbin/shutdown -h now<br></code></pre></div></td></tr></table></figure></li>
<li><p>显示用户与用户组的信息 - <strong>id</strong>。</p>
</li>
<li><p>给其他用户发消息 -<strong>write</strong> / <strong>wall</strong>。</p>
<p>发送方：</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# write hellokitty<br>Dinner is on me.<br>Call me at 6pm.<br></code></pre></div></td></tr></table></figure>

<p>接收方：</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[hellokitty ~]$ <br>Message from root on pts/0 at 17:41 ...<br>Dinner is on me.<br>Call me at 6pm.<br>EOF<br></code></pre></div></td></tr></table></figure></li>
<li><p>查看/设置是否接收其他用户发送的消息 - <strong>mesg</strong>。</p>
</li>
</ol>
   <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[hellokitty ~]$ mesg<br>is y<br>[hellokitty ~]$ mesg n<br>[hellokitty ~]$ mesg<br>is n<br></code></pre></div></td></tr></table></figure>

<h3 id="八：-文件系统"><a href="#八：-文件系统" class="headerlink" title="八： 文件系统"></a>八： 文件系统</h3><h4 id="8-1-文件和路径"><a href="#8-1-文件和路径" class="headerlink" title="8.1 文件和路径"></a>8.1 文件和路径</h4><ol>
<li>命名规则：文件名的最大长度与文件系统类型有关，一般情况下，文件名不应该超过255个字符，虽然绝大多数的字符都可以用于文件名，但是最好使用英文大小写字母、数字、下划线、点这样的符号。文件名中虽然可以使用空格，但应该尽可能避免使用空格，否则在输入文件名时需要用将文件名放在双引号中或者通过<code>\</code>对空格进行转义。</li>
<li>扩展名：在Linux系统下文件的扩展名是可选的，但是使用扩展名有助于对文件内容的理解。有些应用程序要通过扩展名来识别文件，但是更多的应用程序并不依赖文件的扩展名，就像<code>file</code>命令在识别文件时并不是依据扩展名来判定文件的类型。</li>
<li>隐藏文件：以点开头的文件在Linux系统中是隐藏文件（不可见文件）。</li>
</ol>
<h4 id="8-2-目录结构"><a href="#8-2-目录结构" class="headerlink" title="8.2 目录结构"></a>8.2 目录结构</h4><ol>
<li>/bin - 基本命令的二进制文件。</li>
<li>/boot - 引导加载程序的静态文件。</li>
<li>/dev - 设备文件。</li>
<li><strong>/etc</strong> - 配置文件。</li>
<li>/home - 普通用户主目录的父目录。</li>
<li>/lib - 共享库文件。</li>
<li>/lib64 - 共享64位库文件。</li>
<li>/lost+found - 存放未链接文件。</li>
<li>/media - 自动识别设备的挂载目录。</li>
<li>/mnt - 临时挂载文件系统的挂载点。</li>
<li>/opt - 可选插件软件包安装位置。</li>
<li>/proc -  内核和进程信息。</li>
<li><strong>/root</strong> - 超级管理员用户主目录。</li>
<li>/run - 存放系统运行时需要的东西。</li>
<li>/sbin - 超级用户的二进制文件。</li>
<li>/sys - 设备的伪文件系统。</li>
<li>/tmp - 临时文件夹。</li>
<li><strong>/usr</strong> - 用户应用目录。</li>
<li>/var - 变量数据目录。</li>
</ol>
<h4 id="8-3-访问权限"><a href="#8-3-访问权限" class="headerlink" title="8.3 访问权限"></a>8.3 访问权限</h4><ol>
<li><p><strong>chmod</strong> - 改变文件模式比特。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# ls -l<br>...<br>-rw-r--r--  1 root       root 211878 Jun 19 16:06 sohu.html<br>...<br>[root ~]# chmod g+w,o+w sohu.html<br>[root ~]# ls -l<br>...<br>-rw-rw-rw-  1 root       root 211878 Jun 19 16:06 sohu.html<br>...<br>[root ~]# chmod 644 sohu.html<br>[root ~]# ls -l<br>...<br>-rw-r--r--  1 root       root 211878 Jun 19 16:06 sohu.html<br>...<br></code></pre></div></td></tr></table></figure>
<blockquote>
<p>说明：通过上面的例子可以看出，用<code>chmod</code>改变文件模式比特有两种方式：一种是字符设定法，另一种是数字设定法。除了<code>chmod</code>之外，可以通过<code>umask</code>来设定哪些权限将在新文件的默认权限中被删除。</p>
</blockquote>
<p>长格式查看目录或文件时显示结果及其对应权限的数值如下表所示。</p>
<p><img src="https://closer_laps.coding.net/p/picture/d/picture/git/raw/master/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/Linux/file-mode.png" srcset="/img/loading.gif" lazyload></p>
</li>
<li><p><strong>chown</strong> - 改变文件所有者。</p>
 <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# ls -l<br>...<br>-rw-r--r--  1 root root     54 Jun 20 10:06 readme.txt<br>...<br>[root ~]# chown hellokitty readme.txt<br>[root ~]# ls -l<br>...<br>-rw-r--r--  1 hellokitty root     54 Jun 20 10:06 readme.txt<br>...<br></code></pre></div></td></tr></table></figure></li>
<li><p><strong>chgrp</strong> - 改变用户组。</p>
</li>
</ol>
<h4 id="8-4-磁盘管理"><a href="#8-4-磁盘管理" class="headerlink" title="8.4 磁盘管理"></a>8.4 磁盘管理</h4><ol>
<li><p>列出文件系统的磁盘使用状况 - <strong>df</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# df -h<br>Filesystem      Size  Used Avail Use% Mounted on<br>/dev/vda1        40G  5.0G   33G  14% /<br>devtmpfs        486M     0  486M   0% /dev<br>tmpfs           497M     0  497M   0% /dev/shm<br>tmpfs           497M  356K  496M   1% /run<br>tmpfs           497M     0  497M   0% /sys/fs/cgroup<br>tmpfs           100M     0  100M   0% /run/user/0<br></code></pre></div></td></tr></table></figure></li>
<li><p>磁盘分区表操作 - <strong>fdisk</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# fdisk -l<br>Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectors<br>Units = sectors of 1 * 512 = 512 bytes<br>Sector size (logical/physical): 512 bytes / 512 bytes<br>I/O size (minimum/optimal): 512 bytes / 512 bytes<br>Disk label type: dos<br>Disk identifier: 0x000a42f4<br>   Device Boot      Start         End      Blocks   Id  System<br>/dev/vda1   *        2048    83884031    41940992   83  Linux<br>Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 sectors<br>Units = sectors of 1 * 512 = 512 bytes<br>Sector size (logical/physical): 512 bytes / 512 bytes<br>I/O size (minimum/optimal): 512 bytes / 512 bytes<br></code></pre></div></td></tr></table></figure></li>
<li><p>磁盘分区工具 - <strong>parted</strong>。</p>
</li>
<li><p>格式化文件系统 - <strong>mkfs</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# mkfs -t ext4 -v /dev/sdb<br></code></pre></div></td></tr></table></figure>

<ul>
<li><code>-t</code> - 指定文件系统的类型。</li>
<li><code>-c</code> - 创建文件系统时检查磁盘损坏情况。</li>
<li><code>-v</code> - 显示详细信息。</li>
</ul>
</li>
<li><p>文件系统检查 - <strong>fsck</strong>。</p>
</li>
<li><p>转换或拷贝文件 - <strong>dd</strong>。</p>
</li>
<li><p>挂载/卸载 - <strong>mount</strong> / <strong>umount</strong>。</p>
</li>
<li><p>创建/激活/关闭交换分区 - <strong>mkswap</strong> / <strong>swapon</strong> / <strong>swapoff</strong>。</p>
</li>
</ol>
<blockquote>
<p>说明：执行上面这些命令会带有一定的风险，如果不清楚这些命令的用法，最好不用随意使用，在使用的过程中，最好对照参考资料进行操作，并在操作前确认是否要这么做。</p>
</blockquote>
<h3 id="九：-编辑器-vim"><a href="#九：-编辑器-vim" class="headerlink" title="九： 编辑器 - vim"></a>九： 编辑器 - vim</h3><ol>
<li><p>启动vim。可以通过<code>vi</code>或<code>vim</code>命令来启动vim，启动时可以指定文件名来打开一个文件，如果没有指定文件名，也可以在保存的时候指定文件名。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# vim guess.py<br></code></pre></div></td></tr></table></figure></li>
<li><p>命令模式、编辑模式和末行模式：启动vim进入的是命令模式（也称为Normal模式），在命令模式下输入英文字母<code>i</code>会进入编辑模式（Insert模式），屏幕下方出现<code>-- INSERT --</code>提示；在编辑模式下按下<code>Esc</code>会回到命令模式，此时如果输入英文<code>:</code>会进入末行模式，在末行模式下输入<code>q!</code>可以在不保存当前工作的情况下强行退出vim；在命令模式下输入<code>v</code>会进入可视模式（Visual模式），可以用光标选择一个区域再完成对应的操作。</p>
</li>
<li><p>保存和退出vim：在命令模式下输入<code>:</code> 进入末行模式，输入<code>wq</code>可以实现保存退出；如果想放弃编辑的内容输入<code>q!</code>强行退出，这一点刚才已经提到过了；在命令模式下也可以直接输入<code>ZZ</code>实现保存退出。如果只想保存文件不退出，那么可以在末行模式下输入<code>w</code>；可以在<code>w</code>后面输入空格再指定要保存的文件名。</p>
</li>
<li><p>光标操作。</p>
<ul>
<li>在命令模式下可以通过<code>h</code>、<code>j</code>、<code>k</code>、<code>l</code>来控制光标向左、下、上、右的方向移动，可以在字母前输入数字来表示移动的距离，例如：<code>10h</code>表示向左移动10个字符。</li>
<li>在命令模式下可以通过<code>Ctrl+y</code>和<code>Ctrl+e</code>来实现向上、向下滚动一行文本的操作，可以通过<code>Ctrl+f</code>和<code>Ctrl+b</code>来实现向前和向后翻页的操作。</li>
<li>在命令模式下可以通过输入英文字母<code>G</code>将光标移到文件的末尾，可以通过<code>gg</code>将光标移到文件的开始，也可以通过在<code>G</code>前输入数字来将光标移动到指定的行。</li>
</ul>
</li>
<li><p>文本操作。</p>
<ul>
<li>删除：在命令模式下可以用<code>dd</code>来删除整行；可以在<code>dd</code>前加数字来指定删除的行数；可以用<code>d$</code>来实现删除从光标处删到行尾的操作，也可以通过<code>d0</code>来实现从光标处删到行首的操作；如果想删除一个单词，可以使用<code>dw</code>；如果要删除全文，可以在输入<code>:%d</code>（其中<code>:</code>用来从命令模式进入末行模式）。</li>
<li>复制和粘贴：在命令模式下可以用<code>yy</code>来复制整行；可以在<code>yy</code>前加数字来指定复制的行数；可以通过<code>p</code>将复制的内容粘贴到光标所在的地方。</li>
<li>撤销和恢复：在命令模式下输入<code>u</code>可以撤销之前的操作；通过<code>Ctrl+r</code>可以恢复被撤销的操作。</li>
<li>对内容进行排序：在命令模式下输入<code>%!sort</code>。</li>
</ul>
</li>
<li><p>查找和替换。</p>
<ul>
<li>查找操作需要输入<code>/</code>进入末行模式并提供正则表达式来匹配与之对应的内容，例如：<code>/doc.*\.</code>，输入<code>n</code>来向前搜索，也可以输入<code>N</code>来向后搜索。</li>
<li>替换操作需要输入<code>:</code>进入末行模式并指定搜索的范围、正则表达式以及替换后的内容和匹配选项，例如：<code>:1,$s/doc.*/hello/gice</code>，其中：<ul>
<li><code>g</code> - global：全局匹配。</li>
<li><code>i</code> - ignore case：忽略大小写匹配。</li>
<li><code>c</code> - confirm：替换时需要确认。</li>
<li><code>e</code> - error：忽略错误。</li>
</ul>
</li>
</ul>
</li>
<li><p>参数设定：在输入<code>:</code>进入末行模式后可以对vim进行设定。</p>
<ul>
<li><p>设置Tab键的空格数：<code>set ts=4</code></p>
</li>
<li><p>设置显示/不显示行号：<code>set nu</code> / <code>set nonu</code></p>
</li>
<li><p>设置启用/关闭高亮语法：<code>syntax on</code> / <code>syntax off</code></p>
</li>
<li><p>设置显示标尺（光标所在的行和列）： <code>set ruler</code></p>
</li>
<li><p>设置启用/关闭搜索结果高亮：<code>set hls</code> / <code>set nohls</code></p>
<blockquote>
<p>说明：如果希望上面的这些设定在每次启动vim时都能自动生效，需要将这些设定写到用户主目录下的.vimrc文件中。</p>
</blockquote>
</li>
</ul>
</li>
<li><p>高级技巧</p>
<ul>
<li><p>比较多个文件。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# vim -d foo.txt bar.txt<br></code></pre></div></td></tr></table></figure>
<p><img src="https://closer_laps.coding.net/p/picture/d/picture/git/raw/master/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/Linux/vim-diff.png" srcset="/img/loading.gif" lazyload></p>
</li>
<li><p>打开多个文件。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# vim foo.txt bar.txt hello.txt<br></code></pre></div></td></tr></table></figure>

<p>启动vim后只有一个窗口显示的是foo.txt，可以在末行模式中输入<code>ls</code>查看到打开的三个文件，也可以在末行模式中输入<code>b &lt;num&gt;</code>来显示另一个文件，例如可以用<code>:b 2</code>将bar.txt显示出来，可以用<code>:b 3</code>将hello.txt显示出来。</p>
</li>
<li><p>拆分和切换窗口。</p>
<p>可以在末行模式中输入<code>sp</code>或<code>vs</code>来实现对窗口的水平或垂直拆分，这样我们就可以同时打开多个编辑窗口，通过按两次<code>Ctrl+w</code>就可以实现编辑窗口的切换，在一个窗口中执行退出操作只会关闭对应的窗口，其他的窗口继续保留。</p>
<p><img src="https://closer_laps.coding.net/p/picture/d/picture/git/raw/master/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/Linux/vim-multi-window.png" srcset="/img/loading.gif" lazyload></p>
</li>
<li><p>映射快捷键：在vim下可以将一些常用操作映射为快捷键来提升工作效率。</p>
<ul>
<li><p>例子1：在命令模式下输入<code>F4</code>执行从第一行开始删除10000行代码的操作。</p>
<p><code>:map &lt;F4&gt; gg10000dd</code>。</p>
<p>例子2：在编辑模式下输入<code>__main</code>直接补全为<code>if __name__ == &#39;__main__&#39;:</code>。</p>
<p><code>:inoremap __main if __name__ == &#39;__main__&#39;:</code></p>
</li>
</ul>
<blockquote>
<p>说明：上面例子2的<code>inoremap</code>中的<code>i</code>表示映射的键在编辑模式使用， <code>nore</code>表示不要递归，这一点非常重要，否则如果键对应的内容中又出现键本身，就会引发递归（相当于进入了死循环）。如果希望映射的快捷键每次启动vim时都能生效，需要将映射写到用户主目录下的.vimrc文件中。</p>
</blockquote>
</li>
<li><p>录制宏。</p>
<ul>
<li><p>在命令模式下输入<code>qa</code>开始录制宏（其中<code>a</code>是寄存器的名字，也可以是其他英文字母或0-9的数字）。</p>
</li>
<li><p>执行你的操作（光标操作、编辑操作等），这些操作都会被录制下来。</p>
</li>
<li><p>如果录制的操作已经完成了，按<code>q</code>结束录制。</p>
</li>
<li><p>通过<code>@a</code>（<code>a</code>是刚才使用的寄存器的名字）播放宏，如果要多次执行宏可以在前面加数字，例如<code>100@a</code>表示将宏播放100次。</p>
</li>
<li><p>可以试一试下面的例子来体验录制宏的操作，该例子来源于<a target="_blank" rel="noopener" href="https://harttle.land/tags.html#Vim">Harttle Land网站</a>，该网站上提供了很多关于vim的使用技巧，有兴趣的可以了解一下。<br><img src="https://closer_laps.coding.net/p/picture/d/picture/git/raw/master/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/Linux/vim-macro.png" srcset="/img/loading.gif" lazyload></p>
</li>
</ul>
</li>
</ul>
</li>
</ol>
<h3 id="十：-软件安装和配置"><a href="#十：-软件安装和配置" class="headerlink" title="十： 软件安装和配置"></a>十： 软件安装和配置</h3><h4 id="10-1-使用包管理工具"><a href="#10-1-使用包管理工具" class="headerlink" title="10.1 使用包管理工具"></a>10.1 使用包管理工具</h4><ol>
<li><strong>yum</strong> - Yellowdog Updater Modified。<ul>
<li><code>yum search</code>：搜索软件包，例如<code>yum search nginx</code>。</li>
<li><code>yum list installed</code>：列出已经安装的软件包，例如<code>yum list installed | grep zlib</code>。</li>
<li><code>yum install</code>：安装软件包，例如<code>yum install nginx</code>。</li>
<li><code>yum remove</code>：删除软件包，例如<code>yum remove nginx</code>。</li>
<li><code>yum update</code>：更新软件包，例如<code>yum update</code>可以更新所有软件包，而<code>yum update tar</code>只会更新tar。</li>
<li><code>yum check-update</code>：检查有哪些可以更新的软件包。</li>
<li><code>yum info</code>：显示软件包的相关信息，例如<code>yum info nginx</code>。</li>
</ul>
</li>
<li><strong>rpm</strong> - Redhat Package Manager。<ul>
<li>安装软件包：<code>rpm -ivh &lt;packagename&gt;.rpm</code>。</li>
<li>移除软件包：<code>rpm -e &lt;packagename&gt;</code>。</li>
<li>查询软件包：<code>rpm -qa</code>，例如可以用<code>rpm -qa | grep mysql</code>来检查是否安装了MySQL相关的软件包。</li>
</ul>
</li>
</ol>
<p>下面以Nginx为例，演示如何使用yum安装软件。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# yum -y install nginx<br>...<br>Installed:<br>  nginx.x86_64 1:1.12.2-2.el7<br>Dependency Installed:<br>  nginx-all-modules.noarch 1:1.12.2-2.el7<br>  nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7<br>  nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7<br>  nginx-mod-http-perl.x86_64 1:1.12.2-2.el7<br>  nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7<br>  nginx-mod-mail.x86_64 1:1.12.2-2.el7<br>  nginx-mod-stream.x86_64 1:1.12.2-2.el7<br>Complete!<br>[root ~]# yum info nginx<br>Loaded plugins: fastestmirror<br>Loading mirror speeds from cached hostfile<br>Installed Packages<br>Name        : nginx<br>Arch        : x86_64<br>Epoch       : 1<br>Version     : 1.12.2<br>Release     : 2.el7<br>Size        : 1.5 M<br>Repo        : installed<br>From repo   : epel<br>Summary     : A high performance web server and reverse proxy server<br>URL         : http://nginx.org/<br>License     : BSD<br>Description : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and<br>            : IMAP protocols, with a strong focus on high concurrency, performance and low<br>            : memory usage.<br>[root ~]# nginx -v<br>nginx version: nginx/1.12.2<br></code></pre></div></td></tr></table></figure>

<p>移除Nginx。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# yum -y remove nginx<br></code></pre></div></td></tr></table></figure>

<p>下面以MySQL为例，演示如何使用rpm安装软件。要安装MySQL需要先到<a target="_blank" rel="noopener" href="https://www.mysql.com/">MySQL官方网站</a>下载对应的<a target="_blank" rel="noopener" href="https://dev.mysql.com/downloads/mysql/">RPM文件</a>，当然要选择和你使用的Linux系统对应的版本。MySQL现在是Oracle公司旗下的产品，在MySQL被收购后，MySQL的作者重新制作了一个MySQL的分支MariaDB，可以通过yum进行安装。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root mysql]# ls<br>mysql-community-client-5.7.22-1.el7.x86_64.rpm<br>mysql-community-common-5.7.22-1.el7.x86_64.rpm<br>mysql-community-libs-5.7.22-1.el7.x86_64.rpm<br>mysql-community-server-5.7.22-1.el7.x86_64.rpm<br>[root mysql]# yum -y remove mariadb-libs<br>[root mysql]# yum -y install libaio<br>[root mysql]#rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm<br>...<br>[root mysql]#rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm<br>...<br>[root mysql]#rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm<br>...<br>[root mysql]#rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm<br>...<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>说明：由于MySQL和<a target="_blank" rel="noopener" href="https://mariadb.org/">MariaDB</a>的底层依赖库是有冲突的，所以上面我们首先用<code>yum</code>移除了名为mariadb-libs的依赖库并安装了名为libaio支持异步I/O操作的依赖库。关于MySQL和MariaDB之间的关系，可以阅读<a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/MariaDB">维基百科</a>上关于MariaDB的介绍。</p>
</blockquote>
<p>移除安装的MySQL。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# rpm -qa | grep mysql | xargs rpm -e<br></code></pre></div></td></tr></table></figure>

<h4 id="10-2-下载解压配置环境变量"><a href="#10-2-下载解压配置环境变量" class="headerlink" title="10.2 下载解压配置环境变量"></a>10.2 下载解压配置环境变量</h4><p>下面以安装MongoDB为例，演示这类软件应该如何安装。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.5.tgz<br>--2018-06-21 18:32:53--  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.5.tgz<br>Resolving fastdl.mongodb.org (fastdl.mongodb.org)... 52.85.83.16, 52.85.83.228, 52.85.83.186, ...<br>Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|52.85.83.16|:443... connected.<br>HTTP request sent, awaiting response... 200 OK<br>Length: 100564462 (96M) [application/x-gzip]<br>Saving to: ‘mongodb-linux-x86_64-rhel70-3.6.5.tgz’<br><span class="hljs-meta">100%</span><span class="bash">[==================================================&gt;] 100,564,462  630KB/s   <span class="hljs-keyword">in</span> 2m 9s</span><br>2018-06-21 18:35:04 (760 KB/s) - ‘mongodb-linux-x86_64-rhel70-3.6.5.tgz’ saved [100564462/100564462]<br>[root ~]# gunzip mongodb-linux-x86_64-rhel70-3.6.5.tgz<br>[root ~]# tar -xvf mongodb-linux-x86_64-rhel70-3.6.5.tar<br>mongodb-linux-x86_64-rhel70-3.6.5/README<br>mongodb-linux-x86_64-rhel70-3.6.5/THIRD-PARTY-NOTICES<br>mongodb-linux-x86_64-rhel70-3.6.5/MPL-2<br>mongodb-linux-x86_64-rhel70-3.6.5/GNU-AGPL-3.0<br>mongodb-linux-x86_64-rhel70-3.6.5/bin/mongodump<br>mongodb-linux-x86_64-rhel70-3.6.5/bin/mongorestore<br>mongodb-linux-x86_64-rhel70-3.6.5/bin/mongoexport<br>mongodb-linux-x86_64-rhel70-3.6.5/bin/mongoimport<br>mongodb-linux-x86_64-rhel70-3.6.5/bin/mongostat<br>mongodb-linux-x86_64-rhel70-3.6.5/bin/mongotop<br>mongodb-linux-x86_64-rhel70-3.6.5/bin/bsondump<br>mongodb-linux-x86_64-rhel70-3.6.5/bin/mongofiles<br>mongodb-linux-x86_64-rhel70-3.6.5/bin/mongoreplay<br>mongodb-linux-x86_64-rhel70-3.6.5/bin/mongoperf<br>mongodb-linux-x86_64-rhel70-3.6.5/bin/mongod<br>mongodb-linux-x86_64-rhel70-3.6.5/bin/mongos<br>mongodb-linux-x86_64-rhel70-3.6.5/bin/mongo<br>mongodb-linux-x86_64-rhel70-3.6.5/bin/install_compass<br>[root ~]# vim .bash_profile<br>...<br>PATH=$PATH:$HOME/bin:$HOME/mongodb-linux-x86_64-rhel70-3.6.5/bin<br>export PATH<br>...<br>[root ~]# source .bash_profile<br>[root ~]# mongod --version<br>db version v3.6.5<br>git version: a20ecd3e3a174162052ff99913bc2ca9a839d618<br>OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013<br>allocator: tcmalloc<br>modules: none<br>build environment:<br>    distmod: rhel70<br>    distarch: x86_64<br>    target_arch: x86_64<br>[root ~]# mongo --version<br>MongoDB shell version v3.6.5<br>git version: a20ecd3e3a174162052ff99913bc2ca9a839d618<br>OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013<br>allocator: tcmalloc<br>modules: none<br>build environment:<br>    distmod: rhel70<br>    distarch: x86_64<br>    target_arch: x86_64<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>说明：当然也可以通过yum来安装MongoDB，具体可以参照<a target="_blank" rel="noopener" href="https://docs.mongodb.com/master/administration/install-on-linux/">官方网站</a>上给出的说明。</p>
</blockquote>
<h4 id="10-3-源代码构建安装"><a href="#10-3-源代码构建安装" class="headerlink" title="10.3 源代码构建安装"></a>10.3 源代码构建安装</h4><ol>
<li><p>安装Python 3.6。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# yum install gcc<br>[root ~]# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz<br>[root ~]# gunzip Python-3.6.5.tgz<br>[root ~]# tar -xvf Python-3.6.5.tar<br>[root ~]# cd Python-3.6.5<br>[root ~]# ./configure --prefix=/usr/local/python36 --enable-optimizations<br>[root ~]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel<br>[root ~]# make &amp;&amp; make install<br>...<br>[root ~]# ln -s /usr/local/python36/bin/python3.6 /usr/bin/python3<br>[root ~]# python3 --version<br>Python 3.6.5<br>[root ~]# python3 -m pip install -U pip<br>[root ~]# pip3 --version<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>说明：上面在安装好Python之后还需要注册PATH环境变量，将Python安装路径下bin文件夹的绝对路径注册到PATH环境变量中。注册环境变量可以修改用户主目录下的.bash_profile或者/etc目录下的profile文件，二者的区别在于前者相当于是用户环境变量，而后者相当于是系统环境变量。</p>
</blockquote>
</li>
<li><p>安装Redis-3.2.12。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# wget http://download.redis.io/releases/redis-3.2.12.tar.gz<br>[root ~]# gunzip redis-3.2.12.tar.gz<br>[root ~]# tar -xvf redis-3.2.12.tar<br>[root ~]# cd redis-3.2.12<br>[root ~]# make &amp;&amp; make install<br>[root ~]# redis-server --version<br>Redis server v=3.2.12 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=5bc5cd3c03d6ceb6<br>[root ~]# redis-cli --version<br>redis-cli 3.2.12<br></code></pre></div></td></tr></table></figure></li>
</ol>
<h3 id="十一：-配置服务"><a href="#十一：-配置服务" class="headerlink" title="十一： 配置服务"></a>十一： 配置服务</h3><p>我们可以Linux系统下安装和配置各种服务，也就是说我们可以把Linux系统打造成数据库服务器、Web服务器、缓存服务器、文件服务器、消息队列服务器等等。Linux下的大多数服务都被设置为守护进程（驻留在系统后台运行，但不会因为服务还在运行而导致Linux无法停止运行），所以我们安装的服务通常名字后面都有一个字母<code>d</code>，它是英文单词<code>daemon</code>的缩写，例如：防火墙服务叫firewalld，我们之前安装的MySQL服务叫mysqld，Apache服务器叫httpd等。在安装好服务之后，可以使用<code>systemctl</code>命令或<code>service</code>命令来完成对服务的启动、停止等操作，具体操作如下所示。</p>
<ol>
<li><p>启动防火墙服务。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# systemctl start firewalld<br></code></pre></div></td></tr></table></figure></li>
<li><p>终止防火墙服务。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# systemctl stop firewalld<br></code></pre></div></td></tr></table></figure></li>
<li><p>重启防火墙服务。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# systemctl restart firewalld<br></code></pre></div></td></tr></table></figure></li>
<li><p>查看防火墙服务状态。</p>
 <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# systemctl status firewalld<br></code></pre></div></td></tr></table></figure></li>
<li><p>设置/禁用防火墙服务开机自启。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# systemctl enable firewalld<br>Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.<br>Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.<br>[root ~]# systemctl disable firewalld<br>Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.<br>Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.<br></code></pre></div></td></tr></table></figure></li>
</ol>
<h3 id="十二：-计划任务"><a href="#十二：-计划任务" class="headerlink" title="十二： 计划任务"></a>十二： 计划任务</h3><ol>
<li><p>在指定的时间执行命令</p>
<ul>
<li><strong>at</strong> - 将任务排队，在指定的时间执行。</li>
<li><strong>atq</strong> - 查看待执行的任务队列。</li>
<li><strong>atrm</strong> - 从队列中删除待执行的任务。</li>
</ul>
<p>指定3天以后下午5点要执行的任务。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# at 5pm+3days<br><span class="hljs-meta">at&gt;</span><span class="bash"> rm -f /root/*.html</span><br><span class="hljs-meta">at&gt;</span><span class="bash"> &lt;EOT&gt;</span><br>job 9 at Wed Jun  5 17:00:00 2019<br></code></pre></div></td></tr></table></figure>

<p>查看待执行的任务队列。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# atq<br>9       Wed Jun  5 17:00:00 2019 a root<br></code></pre></div></td></tr></table></figure>

<p>从队列中删除指定的任务。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]$ atrm 9<br></code></pre></div></td></tr></table></figure></li>
<li><p>计划任务表 - <strong>crontab</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# crontab -e<br>* * * * * echo &quot;hello, world!&quot; &gt;&gt; /root/hello.txt<br>59 23 * * * rm -f /root/*.log<br></code></pre></div></td></tr></table></figure>
<blockquote>
<p>说明：输入<code>crontab -e</code>命令会打开vim来编辑Cron表达式并指定触发的任务，上面我们定制了两个计划任务，一个是每分钟向/root目录下的hello.txt中追加输出<code>hello, world!</code>；另一个是每天23时59分执行删除/root目录下以log为后缀名的文件。如果不知道Cron表达式如何书写，可以参照/etc/crontab文件中的提示（下面会讲到）或者用搜索引擎找一下“Cron表达式在线生成器”来生成Cron表达式。</p>
</blockquote>
<p>和crontab相关的文件在<code>/etc</code>目录下，通过修改<code>/etc</code>目录下的crontab文件也能够定制计划任务。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# cd /etc<br>[root etc]# ls -l | grep cron<br>-rw-------.  1 root root      541 Aug  3  2017 anacrontab<br>drwxr-xr-x.  2 root root     4096 Mar 27 11:56 cron.d<br>drwxr-xr-x.  2 root root     4096 Mar 27 11:51 cron.daily<br>-rw-------.  1 root root        0 Aug  3  2017 cron.deny<br>drwxr-xr-x.  2 root root     4096 Mar 27 11:50 cron.hourly<br>drwxr-xr-x.  2 root root     4096 Jun 10  2014 cron.monthly<br>-rw-r--r--   1 root root      493 Jun 23 15:09 crontab<br>drwxr-xr-x.  2 root root     4096 Jun 10  2014 cron.weekly<br>[root etc]# vim crontab<br>  1 SHELL=/bin/bash<br>  2 PATH=/sbin:/bin:/usr/sbin:/usr/bin<br>  3 MAILTO=root<br>  4<br>  5 # For details see man 4 crontabs<br>  6<br>  7 # Example of job definition:<br>  8 # .---------------- minute (0 - 59)<br>  9 # |  .------------- hour (0 - 23)<br> 10 # |  |  .---------- day of month (1 - 31)<br> 11 # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...<br> 12 # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat<br> 13 # |  |  |  |  |<br> 14 # *  *  *  *  * user-name  command to be executed<br></code></pre></div></td></tr></table></figure></li>
</ol>
<h3 id="十三：-网络访问和管理"><a href="#十三：-网络访问和管理" class="headerlink" title="十三： 网络访问和管理"></a>十三： 网络访问和管理</h3><ol>
<li><p>安全远程连接 - <strong>ssh</strong>。</p>
 <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]$ ssh root@120.77.222.217<br>The authenticity of host &#x27;120.77.222.217 (120.77.222.217)&#x27; can&#x27;t be established.<br>ECDSA key fingerprint is SHA256:BhUhykv+FvnIL03I9cLRpWpaCxI91m9n7zBWrcXRa8w.<br>ECDSA key fingerprint is MD5:cc:85:e9:f0:d7:07:1a:26:41:92:77:6b:7f:a0:92:65.<br>Are you sure you want to continue connecting (yes/no)? yes<br>Warning: Permanently added &#x27;120.77.222.217&#x27; (ECDSA) to the list of known hosts.<br>root@120.77.222.217&#x27;s password: <br></code></pre></div></td></tr></table></figure></li>
<li><p>通过网络获取资源 - <strong>wget</strong>。</p>
<ul>
<li>-b 后台下载模式</li>
<li>-O 下载到指定的目录</li>
<li>-r 递归下载</li>
</ul>
</li>
<li><p>发送和接收邮件 - <strong>mail</strong>。</p>
</li>
<li><p>网络配置工具（旧） - <strong>ifconfig</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# ifconfig eth0<br>eth0: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500<br>        inet 172.18.61.250  netmask 255.255.240.0  broadcast 172.18.63.255<br>        ether 00:16:3e:02:b6:46  txqueuelen 1000  (Ethernet)<br>        RX packets 1067841  bytes 1296732947 (1.2 GiB)<br>        RX errors 0  dropped 0  overruns 0  frame 0<br>        TX packets 409912  bytes 43569163 (41.5 MiB)<br>        TX errors 0  dropped 0 overruns 0  carrier 0  collisions <br></code></pre></div></td></tr></table></figure></li>
<li><p>网络配置工具（新） - <strong>ip</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# ip address<br>1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN qlen 1<br>    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00<br>    inet 127.0.0.1/8 scope host lo<br>       valid_lft forever preferred_lft forever<br>2: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state UP qlen 1000<br>    link/ether 00:16:3e:02:b6:46 brd ff:ff:ff:ff:ff:ff<br>    inet 172.18.61.250/20 brd 172.18.63.255 scope global eth0<br>       valid_lft forever preferred_lft forever<br></code></pre></div></td></tr></table></figure></li>
<li><p>网络可达性检查 - <strong>ping</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# ping www.baidu.com -c 3<br>PING www.a.shifen.com (220.181.111.188) 56(84) bytes of data.<br>64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=1 ttl=51 time=36.3 ms<br>64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=2 ttl=51 time=36.4 ms<br>64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=3 ttl=51 time=36.4 ms<br>--- www.a.shifen.com ping statistics ---<br>3 packets transmitted, 3 received, 0% packet loss, time 2002ms<br>rtt min/avg/max/mdev = 36.392/36.406/36.427/0.156 ms<br></code></pre></div></td></tr></table></figure></li>
<li><p>显示或管理路由表 - <strong>route</strong>。</p>
</li>
<li><p>查看网络服务和端口 - <strong>netstat</strong> / <strong>ss</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# netstat -nap | grep nginx<br><span class="hljs-meta"></span><br><span class="hljs-meta">#</span><span class="bash"> 端口使用情况</span><br>[root ~]# netstat -ntulp | grep 8080   //查看所有8080端口使用情况<br><span class="hljs-meta"></span><br><span class="hljs-meta">#</span><span class="bash"> 程序占用端口</span><br>[root ~]# ps -ef | grep tomcat //查看tomcat端口占用情况<br></code></pre></div></td></tr></table></figure></li>
<li><p>网络监听抓包 - <strong>tcpdump</strong>。</p>
</li>
<li><p>安全文件拷贝 - <strong>scp</strong>。</p>
</li>
</ol>
  <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# scp root@1.2.3.4:/root/guido.jpg hellokitty@4.3.2.1:/home/hellokitty/pic.jpg<br></code></pre></div></td></tr></table></figure>

<p>  scp中使用sshpass带密码传输文件</p>
  <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">sshpass -p &#x27;密码&#x27; scp -r 用户名@ip:/home/file1 /home/file1<br></code></pre></div></td></tr></table></figure>

<ol start="11">
<li><p>文件同步工具 - <strong>rsync</strong>。</p>
<blockquote>
<p>说明：使用<code>rsync</code>可以实现文件的自动同步，这个对于文件服务器来说相当重要。关于这个命令的用法，我们在后面讲项目部署的时候为大家详细说明。</p>
</blockquote>
</li>
<li><p>安全文件传输 - <strong>sftp</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# sftp root@1.2.3.4<br>root@1.2.3.4&#x27;s password:<br>Connected to 1.2.3.4.<br><span class="hljs-meta">sftp&gt;</span><br></code></pre></div></td></tr></table></figure>

<ul>
<li><p><code>help</code>：显示帮助信息。</p>
</li>
<li><p><code>ls</code>/<code>lls</code>：显示远端/本地目录列表。</p>
</li>
<li><p><code>cd</code>/<code>lcd</code>：切换远端/本地路径。</p>
</li>
<li><p><code>mkdir</code>/<code>lmkdir</code>：创建远端/本地目录。</p>
</li>
<li><p><code>pwd</code>/<code>lpwd</code>：显示远端/本地当前工作目录。</p>
</li>
<li><p><code>get</code>：下载文件。</p>
</li>
<li><p><code>put</code>：上传文件。</p>
</li>
<li><p><code>rm</code>：删除远端文件。</p>
</li>
<li><p><code>bye</code>/<code>exit</code>/<code>quit</code>：退出sftp。</p>
</li>
</ul>
</li>
</ol>
<h3 id="十四：-进程管理"><a href="#十四：-进程管理" class="headerlink" title="十四： 进程管理"></a>十四： 进程管理</h3><ol>
<li><p>查看进程 - <strong>ps</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# ps -ef<br>UID        PID  PPID  C STIME TTY          TIME CMD<br>root         1     0  0 Jun23 ?        00:00:05 /usr/lib/systemd/systemd --switched-root --system --deserialize 21<br>root         2     0  0 Jun23 ?        00:00:00 [kthreadd]<br>...<br>[root ~]# ps -ef | grep mysqld<br>root      4943  4581  0 22:45 pts/0    00:00:00 grep --color=auto mysqld<br>mysql    25257     1  0 Jun25 ?        00:00:39 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid<br></code></pre></div></td></tr></table></figure></li>
<li><p>显示进程状态树 - <strong>pstree</strong>。</p>
 <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# pstree<br>systemd─┬─AliYunDun───18*[&#123;AliYunDun&#125;]<br>        ├─AliYunDunUpdate───3*[&#123;AliYunDunUpdate&#125;]<br>        ├─2*[agetty]<br>        ├─aliyun-service───2*[&#123;aliyun-service&#125;]<br>        ├─atd<br>        ├─auditd───&#123;auditd&#125;<br>        ├─dbus-daemon<br>        ├─dhclient<br>        ├─irqbalance<br>        ├─lvmetad<br>        ├─mysqld───28*[&#123;mysqld&#125;]<br>        ├─nginx───2*[nginx]<br>        ├─ntpd<br>        ├─polkitd───6*[&#123;polkitd&#125;]<br>        ├─rsyslogd───2*[&#123;rsyslogd&#125;]<br>        ├─sshd───sshd───bash───pstree<br>        ├─systemd-journal<br>        ├─systemd-logind<br>        ├─systemd-udevd<br>        └─tuned───4*[&#123;tuned&#125;]<br></code></pre></div></td></tr></table></figure></li>
<li><p>查找与指定条件匹配的进程 - <strong>pgrep</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]$ pgrep mysqld<br>3584<br></code></pre></div></td></tr></table></figure></li>
<li><p>通过进程号终止进程 - <strong>kill</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]$ kill -l<br> 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP<br> 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1<br>11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM<br>16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP<br>21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ<br>26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR<br>31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3<br>38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8<br>43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13<br>48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12<br>53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7<br>58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2<br>63) SIGRTMAX-1  64) SIGRTMAX<br>[root ~]# kill 1234<br>[root ~]# kill -9 1234<br></code></pre></div></td></tr></table></figure>

<p>例子：用一条命令强制终止正在运行的Redis进程。</p>
 <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">ps -ef | grep redis | grep -v grep | awk &#x27;&#123;print $2&#125;&#x27; | xargs kill<br></code></pre></div></td></tr></table></figure></li>
<li><p>通过进程名终止进程 - <strong>killall</strong> / <strong>pkill</strong>。</p>
<p> 结束名为mysqld的进程。</p>
 <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# pkill mysqld<br></code></pre></div></td></tr></table></figure>

<p> 结束hellokitty用户的所有进程。</p>
 <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# pkill -u hellokitty<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>说明：这样的操作会让hellokitty用户和服务器断开连接。</p>
</blockquote>
</li>
<li><p>将进程置于后台运行。</p>
<ul>
<li><code>Ctrl+Z</code> - 快捷键，用于停止进程并置于后台。</li>
<li><code>&amp;</code> - 将进程置于后台运行。</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# mongod &amp;<br>[root ~]# redis-server<br>...<br>^Z<br>[4]+  Stopped                 redis-server<br></code></pre></div></td></tr></table></figure></li>
<li><p>查询后台进程 - <strong>jobs</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# jobs<br>[2]   Running                 mongod &amp;<br>[3]-  Stopped                 cat<br>[4]+  Stopped                 redis-server<br></code></pre></div></td></tr></table></figure></li>
<li><p>让进程在后台继续运行 - <strong>bg</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# bg %4<br>[4]+ redis-server &amp;<br>[root ~]# jobs<br>[2]   Running                 mongod &amp;<br>[3]+  Stopped                 cat<br>[4]-  Running                 redis-server &amp;<br></code></pre></div></td></tr></table></figure></li>
<li><p>将后台进程置于前台 - <strong>fg</strong>。</p>
 <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# fg %4<br>redis-server<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>说明：置于前台的进程可以使用<code>Ctrl+C</code>来终止它。</p>
</blockquote>
</li>
<li><p>调整程序/进程运行时优先级 - <strong>nice</strong> / <strong>renice</strong>。</p>
</li>
<li><p>用户登出后进程继续工作 - <strong>nohup</strong>。</p>
 <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# nohup ping www.baidu.com &gt; result.txt &amp;<br></code></pre></div></td></tr></table></figure></li>
<li><p>跟踪进程系统调用情况 - <strong>strace</strong>。</p>
 <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# pgrep mysqld<br>8803<br>[root ~]# strace -c -p 8803<br>strace: Process 8803 attached<br>^Cstrace: Process 8803 detached<br><span class="hljs-meta">%</span><span class="bash"> time     seconds  usecs/call     calls    errors syscall</span><br>------ ----------- ----------- --------- --------- ----------------<br> 99.18    0.005719        5719         1           restart_syscall<br>  0.49    0.000028          28         1           mprotect<br>  0.24    0.000014          14         1           clone<br>  0.05    0.000003           3         1           mmap<br>  0.03    0.000002           2         1           accept<br>------ ----------- ----------- --------- --------- ----------------<br>100.00    0.005766                     5           total<br></code></pre></div></td></tr></table></figure>

<blockquote>
<p>说明：这个命令的用法和参数都比较复杂，建议大家在真正用到这个命令的时候再根据实际需要进行了解。</p>
</blockquote>
</li>
<li><p>查看当前运行级别 - <strong>runlevel</strong>。</p>
 <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# runlevel<br>N 3<br></code></pre></div></td></tr></table></figure></li>
<li><p>实时监控进程占用资源状况 - <strong>top</strong>。</p>
 <figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# top<br>top - 23:04:23 up 3 days, 14:10,  1 user,  load average: 0.00, 0.01, 0.05<br>Tasks:  65 total,   1 running,  64 sleeping,   0 stopped,   0 zombie<br><span class="hljs-meta">%</span><span class="bash">Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st</span><br>KiB Mem :  1016168 total,   191060 free,   324700 used,   500408 buff/cache<br>KiB Swap:        0 total,        0 free,        0 used.   530944 avail Mem<br>...<br></code></pre></div></td></tr></table></figure>

<ul>
<li><code>-c</code> - 显示进程的整个路径。</li>
<li><code>-d</code> - 指定两次刷屏之间的间隔时间（秒为单位）。</li>
<li><code>-i</code> - 不显示闲置进程或僵尸进程。</li>
<li><code>-p</code> - 显示指定进程的信息。</li>
</ul>
</li>
</ol>
<h3 id="十五：-系统诊断"><a href="#十五：-系统诊断" class="headerlink" title="十五： 系统诊断"></a>十五： 系统诊断</h3><ol>
<li><p>系统启动异常诊断 - <strong>dmesg</strong>。</p>
</li>
<li><p>查看系统活动信息 - <strong>sar</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# sar -u -r 5 10<br>Linux 3.10.0-957.10.1.el7.x86_64 (izwz97tbgo9lkabnat2lo8z)      06/02/2019      _x86_64_        (2 CPU)<br><br>06:48:30 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle<br>06:48:35 PM     all      0.10      0.00      0.10      0.00      0.00     99.80<br><br>06:48:30 PM kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty<br>06:48:35 PM   1772012   2108392     54.33    102816   1634528    784940     20.23    793328   1164704         0<br></code></pre></div></td></tr></table></figure>

<ul>
<li><code>-A</code> - 显示所有设备（CPU、内存、磁盘）的运行状况。</li>
<li><code>-u</code> - 显示所有CPU的负载情况。</li>
<li><code>-d</code> - 显示所有磁盘的使用情况。</li>
<li><code>-r</code> - 显示内存的使用情况。</li>
<li><code>-n</code> - 显示网络运行状态。</li>
</ul>
</li>
<li><p>查看内存使用情况 - <strong>free</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# free<br>              total        used        free      shared  buff/cache   available<br>Mem:        1016168      323924      190452         356      501792      531800<br>Swap:             0           0           0<br></code></pre></div></td></tr></table></figure></li>
<li><p>虚拟内存统计 - <strong>vmstat</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# vmstat<br>procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----<br> r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st<br> 2  0      0 204020  79036 667532    0    0     5    18  101   58  1  0 99  0  0<br></code></pre></div></td></tr></table></figure></li>
<li><p>CPU信息统计 - <strong>mpstat</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# mpstat<br>Linux 3.10.0-957.5.1.el7.x86_64 (iZ8vba0s66jjlfmo601w4xZ)       05/30/2019      _x86_64_        (1 CPU)<br><br>01:51:54 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle<br>01:51:54 AM  all    0.71    0.00    0.17    0.04    0.00    0.00    0.00    0.00    0.00   99.07<br></code></pre></div></td></tr></table></figure></li>
<li><p>查看进程使用内存状况 - <strong>pmap</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# ps<br>  PID TTY          TIME CMD<br> 4581 pts/0    00:00:00 bash<br> 5664 pts/0    00:00:00 ps<br>[root ~]# pmap 4581<br>4581:   -bash<br>0000000000400000    884K r-x-- bash<br>00000000006dc000      4K r---- bash<br>00000000006dd000     36K rw--- bash<br>00000000006e6000     24K rw---   [ anon ]<br>0000000001de0000    400K rw---   [ anon ]<br>00007f82fe805000     48K r-x-- libnss_files-2.17.so<br>00007f82fe811000   2044K ----- libnss_files-2.17.so<br>...<br></code></pre></div></td></tr></table></figure></li>
<li><p>报告设备CPU和I/O统计信息 - <strong>iostat</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# iostat<br>Linux 3.10.0-693.11.1.el7.x86_64 (iZwz97tbgo9lkabnat2lo8Z)      06/26/2018      _x86_64_       (1 CPU)<br>avg-cpu:  %user   %nice %system %iowait  %steal   %idle<br>           0.79    0.00    0.20    0.04    0.00   98.97<br>Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn<br>vda               0.85         6.78        21.32    2106565    6623024<br>vdb               0.00         0.01         0.00       2088          0<br></code></pre></div></td></tr></table></figure></li>
<li><p>显示所有PCI设备 - <strong>lspci</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# lspci<br>00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02)<br>00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]<br>00:01.1 IDE interface: Intel Corporation 82371SB PIIX3 IDE [Natoma/Triton II]<br>00:01.2 USB controller: Intel Corporation 82371SB PIIX3 USB [Natoma/Triton II] (rev 01)<br>00:01.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 03)<br>00:02.0 VGA compatible controller: Cirrus Logic GD 5446<br>00:03.0 Ethernet controller: Red Hat, Inc. Virtio network device<br>00:04.0 Communication controller: Red Hat, Inc. Virtio console<br>00:05.0 SCSI storage controller: Red Hat, Inc. Virtio block device<br>00:06.0 SCSI storage controller: Red Hat, Inc. Virtio block device<br>00:07.0 Unclassified device [00ff]: Red Hat, Inc. Virtio memory balloon<br></code></pre></div></td></tr></table></figure></li>
<li><p>显示进程间通信设施的状态 - <strong>ipcs</strong>。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell">[root ~]# ipcs<br><br>------ Message Queues --------<br>key        msqid      owner      perms      used-bytes   messages    <br><br>------ Shared Memory Segments --------<br>key        shmid      owner      perms      bytes      nattch     status      <br><br>------ Semaphore Arrays --------<br>key        semid      owner      perms      nsems<br></code></pre></div></td></tr></table></figure></li>
</ol>
<h3 id="十六：-Shell编程"><a href="#十六：-Shell编程" class="headerlink" title="十六： Shell编程"></a>十六： Shell编程</h3><p>之前我们提到过，Shell是一个连接用户和操作系统的应用程序，它提供了人机交互的界面（接口），用户通过这个界面访问操作系统内核的服务。Shell脚本是一种为Shell编写的脚本程序，我们可以通过Shell脚本来进行系统管理，同时也可以通过它进行文件操作。总之，编写Shell脚本对于使用Linux系统的人来说，应该是一项标配技能。</p>
<p>互联网上有大量关于Shell脚本的相关知识，我不打算再此对Shell脚本做一个全面系统的讲解，我们通过下面的代码来感性的认识下Shell脚本就行了。</p>
<p>例子1：输入两个整数m和n，计算从m到n的整数求和的结果。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell"><span class="hljs-meta">#</span><span class="bash">!/usr/bin/bash</span><br>printf &#x27;m = &#x27;<br>read m<br>printf &#x27;n = &#x27;<br>read n<br>a=$m<br>sum=0<br>while [ $a -le $n ]<br>do<br>    sum=$[ sum + a ]<br>    a=$[ a + 1 ]<br>done<br>echo &#x27;结果: &#x27;$sum<br></code></pre></div></td></tr></table></figure>

<p>例子2：自动创建文件夹和指定数量的文件。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell"><span class="hljs-meta">#</span><span class="bash">!/usr/bin/bash</span><br>printf &#x27;输入文件名: &#x27;<br>read file<br>printf &#x27;输入文件数量(&lt;1000): &#x27;<br>read num<br>if [ $num -ge 1000 ]<br>then<br>    echo &#x27;文件数量不能超过1000&#x27;<br>else<br>    if [ -e $dir -a -d $dir ]<br>    then<br>        rm -rf $dir<br>    else<br>        if [ -e $dir -a -f $dir ]<br>        then<br>            rm -f $dir<br>        fi<br>    fi<br>    mkdir -p $dir<br>    index=1<br>    while [ $index -le $num ]<br>    do<br>        if [ $index -lt 10 ]<br>        then<br>            pre=&#x27;00&#x27;<br>        elif [ $index -lt 100 ]<br>        then<br>            pre=&#x27;0&#x27;<br>        else<br>            pre=&#x27;&#x27;<br>        fi<br>        touch $dir&#x27;/&#x27;$file&#x27;_&#x27;$pre$index<br>        index=$[ index + 1 ]<br>    done<br>fi<br></code></pre></div></td></tr></table></figure>

<p>例子3：自动安装指定版本的Redis。</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs Shell"><span class="hljs-meta">#</span><span class="bash">!/usr/bin/bash</span><br>install_redis() &#123;<br>    if ! which redis-server &gt; /dev/null<br>    then<br>        cd /root<br>        wget $1$2&#x27;.tar.gz&#x27; &gt;&gt; install.log<br>        gunzip /root/$2&#x27;.tar.gz&#x27;<br>        tar -xf /root/$2&#x27;.tar&#x27;<br>        cd /root/$2<br>        make &gt;&gt; install.log<br>        make install &gt;&gt; install.log<br>        echo &#x27;安装完成&#x27;<br>    else<br>        echo &#x27;已经安装过Redis&#x27;<br>    fi<br>&#125;<br><br>install_redis &#x27;http://download.redis.io/releases/&#x27; $1<br></code></pre></div></td></tr></table></figure>

<h3 id="十七：-开机启动项"><a href="#十七：-开机启动项" class="headerlink" title="十七： 开机启动项"></a>十七： 开机启动项</h3><p>有的时候，经常有自己需要的程序，官方并没有提供添加开机启动的方法，但我们却需要让他们开机启动（比如探针类、飞机类、服务类程序等）。可能有的人会说，可以使用 <code>/etc/rc.local</code> 啊。是可以使用这个，但不是所有的系统都有 <code>rc.local</code> 。今天介绍一个简单的方法，实现将自己需要的程序添加到开机启动项中，并使其在系统启动后自动执行。</p>
<p>首先，进入 <code>/etc/init.d</code> 目录中：</p>
<p><code>cd /etc/init.d/</code></p>
<p>然后，我们使用 <code>ls</code> 命令，会发现里面有一些文件。这些文件就是在开机启动时自动运行的程序。我们现在来开始添加一个自定义的开机启动项 <code>MyTest</code> ：</p>
<p><code>vim MyTest</code></p>
<p>文件名严格区分大小写，请注意！</p>
<p>你可以使用你自己喜欢的文本编辑工具进行编辑工作。此文以 vim 为例。</p>
<p>然后，在继续之前，先看一下开机启动项文件的一个模板：</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash">!/bin/sh</span><br><span class="hljs-meta"></span><br><span class="hljs-meta">#</span><span class="bash"><span class="hljs-comment">## BEGIN INIT INFO</span></span><br><span class="hljs-meta">#</span><span class="bash"> Provides: [程序名称，唯一]</span><br><span class="hljs-meta">#</span><span class="bash"> Required-Start: <span class="hljs-variable">$network</span> <span class="hljs-variable">$remote_fs</span> <span class="hljs-variable">$local_fs</span></span><br><span class="hljs-meta">#</span><span class="bash"> Required-Stop: <span class="hljs-variable">$network</span> <span class="hljs-variable">$remote_fs</span> <span class="hljs-variable">$local_fs</span></span><br><span class="hljs-meta">#</span><span class="bash"> Default-Start: 2 3 4 5</span><br><span class="hljs-meta">#</span><span class="bash"> Default-Stop: 0 1 6</span><br><span class="hljs-meta">#</span><span class="bash"> Short-Description: [启动项的简短说明]</span><br><span class="hljs-meta">#</span><span class="bash"> Description: [启动项的完整说明]</span><br><span class="hljs-meta">#</span><span class="bash"><span class="hljs-comment">## END INIT INFO</span></span><br><br>[需要执行的命令]<br><br>exit 0<br></code></pre></div></td></tr></table></figure>

<p>在这个模板中，<strong>Provides</strong> 是唯一的，也就是在所有的开机启动项中，<strong>Provides</strong> 不能有任何同名冲突。</p>
<p><strong>Short-Description</strong> 和 <strong>Description</strong> 随心情写。</p>
<p>需要执行的命令，按照正常的<code>Bash Shell</code>书写方式书写即可。</p>
<blockquote>
<p>友情提示：需要后台静默运行的程序，请使用 <code>nohup [需要执行的命令] &gt;/dev/null 2&gt;&amp;1 &amp;</code> 方式来启动！</p>
</blockquote>
<p>最后的 <strong>exit 0</strong> 请不要删除！这个是返回正常退出信号的一个传值！</p>
<p>那么，我们设想我们需要创建一个名为 <code>MyTest</code> 的开机启动项，并要求其在开机时在 <code>/root/mytest.txt</code> 文件中写入一句话：“Hello Linux World !”，根据上面的模板，我们需要修改成这个样子：</p>
<figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs SHELL"><span class="hljs-meta">#</span><span class="bash">!/bin/sh</span><br><span class="hljs-meta"></span><br><span class="hljs-meta">#</span><span class="bash"><span class="hljs-comment">## BEGIN INIT INFO</span></span><br><span class="hljs-meta">#</span><span class="bash"> Provides: MyTest</span><br><span class="hljs-meta">#</span><span class="bash"> Required-Start: <span class="hljs-variable">$network</span> <span class="hljs-variable">$remote_fs</span> <span class="hljs-variable">$local_fs</span></span><br><span class="hljs-meta">#</span><span class="bash"> Required-Stop: <span class="hljs-variable">$network</span> <span class="hljs-variable">$remote_fs</span> <span class="hljs-variable">$local_fs</span></span><br><span class="hljs-meta">#</span><span class="bash"> Default-Start: 2 3 4 5</span><br><span class="hljs-meta">#</span><span class="bash"> Default-Stop: 0 1 6</span><br><span class="hljs-meta">#</span><span class="bash"> Short-Description: MyTest</span><br><span class="hljs-meta">#</span><span class="bash"> Description: MyTest Test Program</span><br><span class="hljs-meta">#</span><span class="bash"><span class="hljs-comment">## END INIT INFO</span></span><br><br>echo &quot;Hello Linux World !&quot; &gt;&gt; /root/mytest.txt<br><br>exit 0<br></code></pre></div></td></tr></table></figure>

<p>之后将文件保存，关闭编辑器。</p>
<p><code>chmod +x MyTest</code></p>
<p>然后，将这个启动脚本加入开机启动项中：</p>
<p><code>update-rc.d MyTest defaults</code></p>
<p>然后重新启动系统，或者命令行执行 <code>/etc/init.d/MyTest</code> ，执行启动脚本，然后使用 <code>cat /root/mytest.txt</code>，验证文件是否正确创建。</p>
<p>如何删除我们刚才创建的开机启动项呢？</p>
<p><code>update-rc.d -f MyTest remove</code></p>
<h3 id="十八：-相关资源"><a href="#十八：-相关资源" class="headerlink" title="十八： 相关资源"></a>十八： 相关资源</h3><ol>
<li><p>Linux命令行常用快捷键</p>
<table>
<thead>
<tr>
<th>快捷键</th>
<th>功能说明</th>
</tr>
</thead>
<tbody><tr>
<td>tab</td>
<td>自动补全命令或路径</td>
</tr>
<tr>
<td>Ctrl+a</td>
<td>将光标移动到命令行行首</td>
</tr>
<tr>
<td>Ctrl+e</td>
<td>将光标移动到命令行行尾</td>
</tr>
<tr>
<td>Ctrl+f</td>
<td>将光标向右移动一个字符</td>
</tr>
<tr>
<td>Ctrl+b</td>
<td>将光标向左移动一个字符</td>
</tr>
<tr>
<td>Ctrl+k</td>
<td>剪切从光标到行尾的字符</td>
</tr>
<tr>
<td>Ctrl+u</td>
<td>剪切从光标到行首的字符</td>
</tr>
<tr>
<td>Ctrl+w</td>
<td>剪切光标前面的一个单词</td>
</tr>
<tr>
<td>Ctrl+y</td>
<td>复制剪切命名剪切的内容</td>
</tr>
<tr>
<td>Ctrl+c</td>
<td>中断正在执行的任务</td>
</tr>
<tr>
<td>Ctrl+h</td>
<td>删除光标前面的一个字符</td>
</tr>
<tr>
<td>Ctrl+d</td>
<td>退出当前命令行</td>
</tr>
<tr>
<td>Ctrl+r</td>
<td>搜索历史命令</td>
</tr>
<tr>
<td>Ctrl+g</td>
<td>退出历史命令搜索</td>
</tr>
<tr>
<td>Ctrl+l</td>
<td>清除屏幕上所有内容在屏幕的最上方开启一个新行</td>
</tr>
<tr>
<td>Ctrl+s</td>
<td>锁定终端使之暂时无法输入内容</td>
</tr>
<tr>
<td>Ctrl+q</td>
<td>退出终端锁定</td>
</tr>
<tr>
<td>Ctrl+z</td>
<td>将正在终端执行的任务停下来放到后台</td>
</tr>
<tr>
<td>!!</td>
<td>执行上一条命令</td>
</tr>
<tr>
<td>!数字</td>
<td>执行数字对应的历史命令</td>
</tr>
<tr>
<td>!字母</td>
<td>执行最近的以字母打头的命令</td>
</tr>
<tr>
<td>!$ / Esc+.</td>
<td>获得上一条命令最后一个参数</td>
</tr>
<tr>
<td>Esc+b</td>
<td>移动到当前单词的开头</td>
</tr>
<tr>
<td>Esc+f</td>
<td>移动到当前单词的结尾</td>
</tr>
</tbody></table>
</li>
<li><p>man查阅命令手册的内容说明</p>
<table>
<thead>
<tr>
<th>手册中的标题</th>
<th>功能说明</th>
</tr>
</thead>
<tbody><tr>
<td>NAME</td>
<td>命令的说明和介绍</td>
</tr>
<tr>
<td>SYNOPSIS</td>
<td>使用该命令的基本语法</td>
</tr>
<tr>
<td>DESCRIPTION</td>
<td>使用该命令的详细描述，各个参数的作用，有时候这些信息会出现在OPTIONS中</td>
</tr>
<tr>
<td>OPTIONS</td>
<td>命令相关参数选项的说明</td>
</tr>
<tr>
<td>EXAMPLES</td>
<td>使用该命令的参考例子</td>
</tr>
<tr>
<td>EXIT STATUS</td>
<td>命令结束的退出状态码，通常0表示成功执行</td>
</tr>
<tr>
<td>SEE ALSO</td>
<td>和命令相关的其他命令或信息</td>
</tr>
<tr>
<td>BUGS</td>
<td>和命令相关的缺陷的描述</td>
</tr>
<tr>
<td>AUTHOR</td>
<td>该命令的作者介绍</td>
</tr>
</tbody></table>
</li>
</ol>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/">读书笔记</a>
                    
                      <a class="hover-with-bg" href="/categories/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/Linux/">Linux</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Linux/">Linux</a>
                    
                      <a class="hover-with-bg" href="/tags/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/">读书笔记</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/02/23/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/SQL/%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93MySQL/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">关系数据库MySQL入门</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/02/23/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/Python/%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%92%8C%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/">
                        <span class="hidden-mobile">使用正则表达式</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js', function () {
        new Valine({
          el: "#valine",
          app_id: "YzLqNtMw1YEwwACli1FUsIUM-gzGzoHsz",
          app_key: "HLUt5izfTvTcbEbOrA59W92a",
          placeholder: "畅所欲言...",
          path: window.location.pathname,
          avatar: "robohash",
          meta: ["nick","mail","link"],
          pageSize: "10",
          lang: "zh-CN",
          highlight: true,
          recordIP: false,
          serverURLs: "",
        });
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->

  <div class="col-lg-7 mx-auto nopadding-x-md">
    <div class="container custom post-custom mx-auto">
      <img src="https://closer_laps.coding.net/p/picture/d/picture/git/raw/master/pay/pay.png" srcset="/img/loading.gif" lazyload class="rounded mx-auto d-block mt-3" style="width:355.4px; height:200px;">
    </div>
  </div>


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- LeanCloud 统计PV -->
        <span id="leancloud-site-pv-container" style="display: none">
            总访问量 
            <span id="leancloud-site-pv"></span>
             次
          </span>
      
      
        <!-- LeanCloud 统计UV -->
        <span id="leancloud-site-uv-container" style="display: none">
            总访客数 
            <span id="leancloud-site-uv"></span>
             人
          </span>
      

    
  </div>


  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        苏ICP备20032307号
      </a>
    </span>
    
      
        <span>
          <a
            href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=32020602001023"
            rel="nofollow noopener"
            class="beian-police"
            target="_blank"
          >
            
              <span style="visibility: hidden; width: 0">|</span>
              <img src="/img/police_beian.png" srcset="/img/loading.gif" lazyload alt="police-icon"/>
            
            <span>苏公网安备 32020602001023号</span>
          </a>
        </span>
      
    
  </div>


  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.2/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.0/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>




  <script defer src="/js/leancloud.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "/local-search.xml";
      $('#local-search-input').on('click', function() {
        searchFunc(path, 'local-search-input', 'local-search-result');
      });
      $('#modalSearch').on('shown.bs.modal', function() {
        $('#local-search-input').focus();
      });
    })()
  </script>





  

  
    <!-- MathJax -->
    <script>
      MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']]
        },
        options: {
          renderActions: {
            findScript: [10, doc => {
              document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
                const display = !!node.type.match(/; *mode=display/);
                const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
                const text = document.createTextNode('');
                node.parentNode.replaceChild(text, node);
                math.start = { node: text, delim: '', n: 0 };
                math.end = { node: text, delim: '', n: 0 };
                doc.math.push(math);
              });
            }, '', false],
            insertedScript: [200, () => {
              document.querySelectorAll('mjx-container').forEach(node => {
                let target = node.parentNode;
                if (target.nodeName.toLowerCase() === 'li') {
                  target.parentNode.classList.add('has-jax');
                }
              });
            }, '', false]
          }
        }
      };
    </script>

    <script async src="https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/tex-svg.js" ></script>

  








  
    <!-- Baidu Analytics -->
    <script defer>
      var _hmt = _hmt || [];
      (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?608f2baddd361128381ad2bf9377bf89";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
  

  

  

  

  

  





<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
